CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  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 -

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