![]() |
Map C++ feature to C
Dear friends
Is there a way to embulate C++'s virtual destructors in pure C. IE just using function pointers. Many Thanks. |
Re: Map C++ feature to C
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 |
Re: Map C++ feature to C
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. |
Re: Map C++ feature to C
On Jun 24, 9:23*am, zgene <nos...@nospam.com> 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. |
Re: Map C++ feature to C
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?? |
Re: Map C++ feature to C
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 |
Re: Map C++ feature to C
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 |
Re: Map C++ feature to C
Paul N於 2012年6月25日星期一UTC+8上午3時19分00秒 寫道:
> On Jun 24, 9:23*am, zgene <nos...@nospam.com> 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++. |
Re: Map C++ feature to C
> 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. |
Re: Map C++ feature to C
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 . |
| All times are GMT. The time now is 03:14 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.