• Advertisement
Sign in to follow this  

C++ inheriting from templated class question

This topic is 2504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to do something like this down below.


template <typename T_InfoStruct>
class BaseClass {
T_InfoStruct * thing;
};

class SubClass: public BaseClass<SubClass::InnerPrivateStruct>
{
private:
struct InnerPrivateStruct {
blah;
};
};


I want to encapsulate the InnerPrivateStruct within class Subclass and I want to have it be the template paramater as well to the templated class SubClass is inheriting from. I tried forward declaring it among other things and nothing works. Won't compile. For now I'm leaving the struct exposed outside which isn't harmful or anything. I feel like there has to be a way to do this though.

Share this post


Link to post
Share on other sites
Advertisement
This compiled for me, no idea if it would work though.




template<class T>
class C
{
T *t;
};

class B;

class A: public C<A::B>
{
class B
{
};
int a;
};



edit: Although it compiled, when I try to assign an A::B object to "t" it thinks t is a "B" object and NOT a A::B object. Perhaps you should use composition and friend.



template<class T>
class C
{
public:
T *t;
};

class A;
class B
{
friend A;
B(){};
~B(){}
};

class A: public C<B>
{
public:
A()
{
t = new B;
}
~A()
{
delete t;
}
};



Although B can be seen by other things, it can't actually be used by anyhting other than A.

Share this post


Link to post
Share on other sites

This compiled for me, no idea if it would work though.




template<class T>
class C
{
T *t;
};

class B;

class A: public C<A::B>
{
class B
{
};
int a;
};



edit: Although it compiled, when I try to assign an A::B object to "t" it thinks t is a "B" object and NOT a A::B object. Perhaps you should use composition and friend.


Hmm there isn't really any point in using composition in this case since it's just a struct. It holds like a chunk of data and doesn't do any work.

Share this post


Link to post
Share on other sites
Maybe just give the Innerstruct a name showing that its part of SubClass like so :

template<typename T>
struct Base{
T * p;
}

struct _subClass_DataHolder_Helper{
//...
}

struct SubClass : Base<_subClass_DataHolder_Helper>{
};


or just compose _subClass_DataHolder_Helper inside SubClass. But [color=#2B3730]Nanoha solution would give you similar effect as you wanted, that is to make _subClass_DataHolder_Helper not applicable to others except SubClass.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement