CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Inheriting pointers in derived class

    Hello to all.

    I write you after many years, got rusty, even more than then, on inheritances.

    I will pose a simplified version of the problem i have, to go straight to the point.
    I have a base class, baseclass, which has three public members, pointers to baseclass class; they are links in a node structure, and are protected for access security reason.
    I have a function that retrieves these pointers, GetPtr, parametrized on the pointer to get (in the full program, there is a dynamic allocated array of pointers). This function is publicly accessible

    Code:
    class baseclass
    {
    //...
    protected: 
    baseclass *ptr1, *ptr2, *ptr3;
    
    public:
    baseclass* GetPtr(unsigned char numeral);
    
    //...
    }
    The problem is, when I derive a class from baseclass, let's say derivedclass, and call GetPtr() I obviously retrieve the pointer to the baseclass, while I need to obtain an inherited version of GetPtr that returns the correct derived class pointer type.

    I could define GetPtr as virtual and override it in all derived classes, but theese can be very numerous, and this doesn't solve my problem.

    Is there a way to achieve this goal (I think dynamic cast could help, but is it dangerous?)?

    Thanks you all in advance!
    Last edited by Buzzyous; April 20th, 2012 at 09:10 PM.
    - Buzzyous -

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Inheriting pointers in derived class

    Quote Originally Posted by Buzzyous View Post
    The problem is, when I derive a class from baseclass, let's say derivedclass, and call GetPtr() I obviously retrieve the pointer to the baseclass, while I need to obtain an inherited version of GetPtr that returns the correct derived class pointer type.
    You didn't fully explain what ptr1, ptr2, and ptr3 denote, and the explanation of GetPtr() says nothing about ptr1, ptr2, or ptr3.

    Will these pointers actually be pointing to derived instances of baseclass? You also should explain exactly what GetPtr() returns. Does it return ptr1, ptr2, or ptr3? If so, then there is nothing wrong with GetPtr() as-is if ptr1, ptr2, or ptr3 will be actually pointing to derived objects.
    Is there a way to achieve this goal (I think dynamic cast could help, but is it dangerous?)?
    It isn't "dangerous" -- however what it does do is defeat the purpose or polymorphism by making you have to write if() or switch-case statements everywhere you need to check the pointer type. The dynamic_cast should be used sparingly -- using it is usually a sign of a design flaw.

    Maybe your problem can really be solved with templates and not inheritance. We need to see much more of your design to determine this.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: Inheriting pointers in derived class

    Yes, templates could work fine.

    Thanks very much!

    Sorry if my answer came late
    - Buzzyous -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured