Re: Polymorphism: via base pointer getting the original object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Andrew Haley via Gcc-help wrote on 04/26/23 19:35:
On 4/26/23 18:20, U.Mutlu wrote:
How best to do this?

In a word or two, don't. This is basic OOP: all accesses to an object should
be via its (possibly virtual) member functions. There's nothing to stop you
from defining

   virtual Dog_t Base_t::as_dog() { return nullptr; }

in the base class, and overriding it as

   virtual Dog_t Dog_t::as_dog() { return this; }

in the derived class. If you really need to...

Thx, just tried this out, but it's not of much help as one has to know the type in advance, ie. at compile time.

Can I remind you that this list is for specific GCC help, not C++ language
issues?

Thanks.

Oh, sorry, I forgot. Can you recommend a mailing list where such issues are discussed? Thx.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux