Given the initial comments posted to the thread, I expected this thread to die, but it has not. I thought that moderator might move the thread to the General thread, but it is still here, so....
You can directly call a method for a specific class in C++ providing you have visibility to see the class and the method in the class. The example that I provided to the original poster using a direct email is as follows:
class LTop
{
public:
virtual void notInMid() const
{
qDebug("Method LTop::notInMode()");
}
virtual void allDo() const
{
qDebug("Method LTop::allDo()");
}
};
class LMid : public LTop
{
typedef LTop Base;
public:
virtual void allDo() const
{
Base::allDo();
qDebug("Method LMid::allDo()");
}
};
class LBottom : public LMid
{
typedef LMid Base;
public:
virtual void notInMid() const
{
//Cannot make this call because it does not exist
//Base::notInMid();
//Only works if there is public inheritance.
LTop::notInMid();
qDebug("Method LBottom::notInMode()");
}
virtual void allDo() const
{
//this->LMid::allDo();
Base::allDo();
qDebug("Method LBottom::allDo()");
}
};
class LTop
{
public:
virtual void notInMid() const
{
qDebug("Method LTop::notInMode()");
}
virtual void allDo() const
{
qDebug("Method LTop::allDo()");
}
};
class LMid : public LTop
{
typedef LTop Base;
public:
virtual void allDo() const
{
Base::allDo();
qDebug("Method LMid::allDo()");
}
};
class LBottom : public LMid
{
typedef LMid Base;
public:
virtual void notInMid() const
{
//Cannot make this call because it does not exist
//Base::notInMid();
//Only works if there is public inheritance.
LTop::notInMid();
qDebug("Method LBottom::notInMode()");
}
virtual void allDo() const
{
//this->LMid::allDo();
Base::allDo();
qDebug("Method LBottom::allDo()");
}
};
To copy to clipboard, switch view to plain text mode
Notice a few things that allow this to work
- Public inheritance is used. Using "class LMid : LTop" rather than "class LMid : public LTop" prevents LBottom from referencing a method in LTop, but still allows LMid to reference LTop.
- The methods must have visibility. In this example, I used public:
- You cannot call a method directly that does not exist. Consider the method "notInMid". The method is not implemented in LMid. I dislike that in LBottom I cannot simply say "call the next one up in the heirarchy". I am sure that this is because C++ supports multiple inheritance, so it is not obvious what to call unless you explicitly specify it.
The probably enough for now.
Bookmarks