Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Map C++ feature to C

Reply
Thread Tools

Map C++ feature to C

 
 
BGB
Guest
Posts: n/a
 
      06-25-2012
On 6/24/2012 5:15 PM, Ian Collins wrote:
> On 06/25/12 06:39 AM, BGB wrote:
>> On 6/24/2012 4:25 AM, Ian Collins wrote:
>>> On 06/24/12 08:23 PM, zgene wrote:
>>>> Dear friends
>>>
>>> Why is the question completely different form the subject?
>>>
>>>> Is there a way to embulate C++'s virtual destructors in pure C. IE just
>>>> using function pointers.
>>>
>>> No, because there isn't a way to fully emulate destructors.
>>>

>>
>> strictly speaking, yes.
>>
>> but if one allows for the case where the destructor is called via the
>> object being manually destroyed/"deleted", then it becomes a little
>> easier: just have it as a method/function-pointer which is called prior
>> to freeing the memory.

>
> A "little" easier is the appropriate term. The OP asked for virtual
> destructor functionality, which requires some form of virtual dispatch
> mechanism. Doable, but ugly.
>


IMO, using C function pointers are not that much drastically worse than
using C++ virtual methods.

yes, one either needs to use a vtable, or assign all of the "methods" on
object construction, but this isn't really a huge issue IMO.


say, we have something like:
BaseClass *obj;
....
obj=(BaseClass *)FooClass_New(); //FooClass extends BaseClass
....
BaseClass_Delete(obj); //internally calls FooClass destructor

with, say:
void BaseClass_Delete(BaseClass *obj)
{ obj->vt->Destroy(obj); myapp_free(obj); }


but, hell, it works...

 
Reply With Quote
 
 
 
 
BGB
Guest
Posts: n/a
 
      06-25-2012
On 6/25/2012 5:34 AM, Jorgen Grahn wrote:
> On Mon, 2012-06-25, Leo Havm°ller wrote:
>>> Why would you want to do that? If you want to write C++ code, there is
>>> an excellent language for that - it is called C++. Much better than
>>> some half-arsed attempt to simulate it in C.

>>
>> Because C++ may not be allowed (e.g. linux kernel mode) or available (e.g.
>> linux IAD) for the target platform.

>
> If C++ is disallowed, half-arsed simulations should be, too.
> Especially simulations of tricky features like run-time polymorphism.
>
> (Note: I'm not arguing against all uses of function pointers. Just
> against unrestricted use of idioms meant to simulate C++ vtables.)
>


it depends on why it is disallowed.

it may not be due entirely to "policy", but maybe something like lack of
a good C++ compiler, or the relative difficulty to parse C++ code (vs C)
via custom-written tools, or interface directly between C++ and a
scripting language (many have an easier time interfacing with C), ...

 
Reply With Quote
 
 
 
 
nroberts
Guest
Posts: n/a
 
      07-23-2012
On Sunday, June 24, 2012 3:12:18 PM UTC-7, Ian Collins wrote:
> On 06/25/12 09:33 AM, zgene wrote:
> > On Sun, 24 Jun 2012 21:25:38 +1200, Ian Collins wrote:
> >
> >> On 06/24/12 08:23 PM, zgene wrote:
> >>> Dear friends
> >>
> >> Why is the question completely different form the subject?
> >>
> >>> Is there a way to embulate C++'s virtual destructors in pure C. IE just
> >>> using function pointers.
> >>
> >> No, because there isn't a way to fully emulate destructors.
> >
> > I doubt this answer..... do you have any evidence??
>
> Show me a way to automatically call a function on leaving a scope in C
> and we can go from there.


This is probably THE most important feature of C++ missing from standard C but it can be done with GCC extensions:

type var __attribute__(cleanup(destroyType)) = newType();

 
Reply With Quote
 
Andrew Cooper
Guest
Posts: n/a
 
      07-24-2012
On 23/07/2012 18:25, nroberts wrote:
> On Sunday, June 24, 2012 3:12:18 PM UTC-7, Ian Collins wrote:
>> On 06/25/12 09:33 AM, zgene wrote:
>> > On Sun, 24 Jun 2012 21:25:38 +1200, Ian Collins wrote:
>> >
>> >> On 06/24/12 08:23 PM, zgene wrote:
>> >>> Dear friends
>> >>
>> >> Why is the question completely different form the subject?
>> >>
>> >>> Is there a way to embulate C++'s virtual destructors in pure C. IE just
>> >>> using function pointers.
>> >>
>> >> No, because there isn't a way to fully emulate destructors.
>> >
>> > I doubt this answer..... do you have any evidence??
>>
>> Show me a way to automatically call a function on leaving a scope in C
>> and we can go from there.

>
> This is probably THE most important feature of C++ missing from standard C but it can be done with GCC extensions:
>
> type var __attribute__(cleanup(destroyType)) = newType();
>


It is not any feature "missing" from the C standard. C is not C++, nor
should it be. gcc extensions like this are frankly a bastardisation of
C, and should be discouraged.

C, unlike C++ has only one possible interpretation for any valid
construct. This makes is substantially more predictable, and reduces
many overheads of the more complex features.

Take the following C++ code:

Foo f = Foo(g);

Care to explain exactly what that statement means? What would be the
equivalent in meaning for C?

~Andrew
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-24-2012
On Tue, 2012-07-24, Andrew Cooper wrote:
> On 23/07/2012 18:25, nroberts wrote:

....
>>
>> This is probably THE most important feature of C++ missing from standard C
>> but it can be done with GCC extensions:
>>
>> type var __attribute__(cleanup(destroyType)) = newType();
>>

>
> It is not any feature "missing" from the C standard. C is not C++, nor
> should it be. gcc extensions like this are frankly a bastardisation of
> C, and should be discouraged.


I tend to agree.

> C, unlike C++ has only one possible interpretation for any valid
> construct. This makes is substantially more predictable, and reduces
> many overheads of the more complex features.


Overheads? In compiler complexity, runtime, the reader's head ... ?

> Take the following C++ code:
>
> Foo f = Foo(g);
>
> Care to explain exactly what that statement means? What would be the
> equivalent in meaning for C?


It means you're left with a Foo object called f, constructed via
Foo(g), doesn't it? I'm assuming a reasonable implementation of Foo.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
aftnix
Guest
Posts: n/a
 
      07-26-2012
On Sunday, June 24, 2012 2:23:02 PM UTC+6, zgene wrote:
> Dear friends
>
> Is there a way to embulate C++'s virtual destructors in pure C. IE just
> using function pointers.
>
> Many Thanks.


Hi,

Have you looked into this?

http://www.planetpdf.com/codecuts/pdfs/ooc.pdf
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::map::find() throws exception when map is empty? Matthias Hildebrand C++ 5 03-20-2012 06:09 AM
Real-world use cases for map's None fill-in feature? Raymond Hettinger Python 30 01-25-2006 07:30 AM
Do you have real-world use cases for map's None fill-in feature? Raymond Hettinger Python 12 01-10-2006 05:51 PM
I can map all files (.*) to asp.net worker.How do I map NO FILE to asp.net worker? alex ASP .Net 1 02-04-2005 03:18 AM
Map feature in thunderbird address book Ranjit Firefox 1 07-31-2004 02:39 AM



Advertisments