|
-
April 20th, 2012, 08:13 PM
#1
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 -
-
April 21st, 2012, 12:46 AM
#2
Re: Inheriting pointers in derived class
 Originally Posted by Buzzyous
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
-
April 21st, 2012, 04:47 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|