Map C++ feature to C

Z

zgene

Dear friends

Is there a way to embulate C++'s virtual destructors in pure C. IE just
using function pointers.

Many Thanks.
 
I

Ian Collins

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.
 
B

BGB

Why is the question completely different form the subject?


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.
 
P

Paul N

Dear friends

Is there a way to embulate C++'s virtual destructors in pure C. IE just
using function pointers.

Many Thanks.

How about something like:

struct Base {
void *destroy(struct Base *this); // needs to be first member
/// more here
};

struct Derived {
struct Base b;
// even more here
};

When you create a Base or a Derived, you set the function pointer to
the correct destructor. Then just before freeing an object, you do:

(p -> destroy) (p);

which will work whether p actually points to a Base or a Derived.

... completely untested, syntax may be wrong as well...

Hope that helps.
Paul.
 
Z

zgene

Why is the question completely different form the subject?


No, because there isn't a way to fully emulate destructors.

I doubt this answer..... do you have any evidence??
 
I

Ian Collins

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.
 
8

88888 Dihedral

Paul Næ–¼ 2012å¹´6月25日星期一UTC+8上åˆ3時19分00秒寫é“:
How about something like:

struct Base {
void *destroy(struct Base *this); // needs to be first member
/// more here
};

struct Derived {
struct Base b;
// even more here
};

When you create a Base or a Derived, you set the function pointer to
the correct destructor. Then just before freeing an object, you do:

(p -> destroy) (p);

which will work whether p actually points to a Base or a Derived.

... completely untested, syntax may be wrong as well...

Hope that helps.
Paul.


Well, that means it might lead to
the results of destroying forrests from time
to time in my understanding
of the object oriented approach in C++.
 
L

Leo Havmøller

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.

Leo Havmøller.
 
J

Jorgen Grahn

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.)

/Jorgen
 
B

BGB

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...
 
B

BGB

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), ...
 
N

nroberts

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();
 
A

Andrew Cooper

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
 
J

Jorgen Grahn

On 23/07/2012 18:25, nroberts wrote: ....

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top