Reputation: 7136
Easier to show than explain in only words
class OutterBase{
protected:
virtual ItemList buildDefaultItemListForCatagory1();
class Inner{
ItemList catagory1, catagory2;
public:
void Inner(){ _initOnce(); }
private:
void _initOnce(){
/* want to be able call the virtual buildDefaultItemListForCatagory1 */
}
}
typedef Singleton<Inner> InnerClassType; //has static getInstance method to get Inner object
}
class OutterDerived: public OutterBase{
virtual ItemList buildDefaultItemListForCatagory1();
}
So that's the situation right now. I want to be able to call the virtual buildDefaultItemListForCatagory1 inside Inner::_initOnce function. Is there a way to do this.
If not possible, then I need to redesign this. I want to only have one instance of Inner class in OutterBase and have it available to any derived class. I also need it to be constructed depending on the virtual buildDefault* function. Can someone suggest an alternative good solution if the above is not achievable? Thanks.
Upvotes: 0
Views: 141
Reputation: 6668
You can.
A nested (inner) class is a member with all the access rights of a normal member and thus has access to the protected and private members of its outer class.
The question is: call it on which object?
Upvotes: 1
Reputation: 613
You can pass the outter object to the inner class constructor and do something like this:
class OutterBase{
protected:
virtual ItemList buildDefaultItemListForCatagory1();
class Inner{
ItemList catagory1, catagory2;
OutterBase *outter;
public:
Inner(OutterBase *_outter){ outter=_outter;_initOnce(); }
private:
void _initOnce(){
outter->buildDefaultItemListForCatagory1();
}
}
typedef Singleton<Inner> InnerClassType; //has static getInstance method to get Inner object
}
Upvotes: 0