Sign in to follow this  
ill

C++ inheriting from templated class question

Recommended Posts

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

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

class SubClass: public BaseClass<SubClass::InnerPrivateStruct>
{
private:
struct InnerPrivateStruct {
blah;
};
};
[/code]

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
Nanoha    2682
This compiled for me, no idea if it would work though.

[size="2"]
[code]

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

class B;

class A: public C<A::B>
{
class B
{
};
int a;
};
[/code][/size][size="2"]


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.[/size] Perhaps you should use composition and friend.

[code]

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;
}
};

[/code]

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
ill    320
[quote name='Nanoha' timestamp='1302986702' post='4799254']
This compiled for me, no idea if it would work though.

[size="2"]
[code]

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

class B;

class A: public C<A::B>
{
class B
{
};
int a;
};
[/code][/size][size="2"]


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.[/size] Perhaps you should use composition and friend.
[/quote]

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
Concentrate    181
Maybe just give the Innerstruct a name showing that its part of SubClass like so :
[code]
template<typename T>
struct Base{
T * p;
}

struct _subClass_DataHolder_Helper{
//...
}

struct SubClass : Base<_subClass_DataHolder_Helper>{
};
[/code]

or just compose _subClass_DataHolder_Helper inside SubClass. But [color=#2B3730][b]Nanoha[/b][/color] 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this