Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Map C++ feature to C

 
 
zgene
Guest
Posts: n/a
 
      06-24-2012
Dear friends

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

Many Thanks.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-24-2012
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.

--
Ian Collins
 
Reply With Quote
 
 
 
 
BGB
Guest
Posts: n/a
 
      06-24-2012
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.

 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      06-24-2012
On Jun 24, 9:23*am, zgene <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
zgene
Guest
Posts: n/a
 
      06-24-2012
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??
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-24-2012
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.

--
Ian Collins
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-24-2012
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.

--
Ian Collins
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      06-25-2012
Paul N於 2012年6月25日星期一UTC+8上午3時19分00秒 寫道:
> On Jun 24, 9:23*am, zgene <(E-Mail Removed)> wrote:
> > 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.



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


 
Reply With Quote
 
Leo Havmller
Guest
Posts: n/a
 
      06-25-2012
> 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 Havmller.

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      06-25-2012
On Mon, 2012-06-25, Leo Havmller 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.)

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
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