Kinda like this... I guess the two classes (myClass1 & myClass2) need to inherit the same class or something like that. Is this possible? Is it very complicated? If it is just say so
I guess this is a really weird way of accomplishing what I want and I don't really need this so if it isn't possible or very hard it doesn't matter, I guess i'm still a beginner
Yes it's possible, yes they need to inherit the same base class. No, it's not very complicated, but yes it may be somewhat more advanced than your current level. Note that that doesn't mean to shirk back, but rather that you should stick your head down and plow stubbornly forward when you encounter problems, by keep asking questions until you understand it. ("Above your level" means, "Get out your climbing gear", not "Go do something else"). (
If it's too confusing, that may just be because you haven't yet learned some other concepts that this is built upon, and that you should learn those first, but to mark this concept as one to look forward to learning later)
If you have a base class,
MyBase, and two classes that inherit it,
DerivedA and
DerivedB, a
MyBase pointer or reference can actually point to a
DerivedA and
DerivedB - Or actually, they can only point to the
MyBase inherited part of
DerivedA and
DerivedB (Since
DerivedA and
DerivedB contain
MyBase in it, since they inherited it), but not point at
DerivedA and
DerivedB themselves.
What good is that, then? Well, if
MyBase has a virtual function, let's call it
MyVirtualFunc(), and DerivedA and DerivedB overload that virtual function with their own
MyVirtualFunc() (taking the exact same parameters and etc...), when you call the "
MyBase pointer to DerivedA" MyVirtualFunc() function, it'll actually call
DerivedA's overload of it. This is because the function is
virtual, and won't behave the same way if it's non-virtual. This only works because DerivedA and DerivedB both inherit MyBase.
Here's an example of that:
class MyBase
{
public:
virtual void MyVirtualFunc() { std::cout << "MyBase::MyVirtualFunc() was called." << std::endl;}
};
class DerivedA : public MyBase
{
public:
void MyVirtualFunc() { std::cout << "DerivedA::MyVirtualFunc() was called." << std::endl;}
};
class DerivedB : public MyBase
{
public:
void MyVirtualFunc() { std::cout << "DerivedB::MyVirtualFunc() was called." << std::endl;}
};
int main()
{
MyBase myBase;
DerivedA derivedA;
DerivedB derivedB;
MyBase *myBasePtr = &myBase; //Use a MyBase pointer to point to a MyBase.
myBasePtr->MyVirtualFunc(); //Uses MyBase's version.
myBasePtr = &derivedA; //Use a MyBase pointer to point to the 'MyBase' inherited part of the DerivedA.
myBasePtr->MyVirtualFunc(); //Uses DerivedA's version.
myBasePtr = &derivedB; //Use a MyBase pointer to point to the 'MyBase' inherited part of the DerivedB.
myBasePtr->MyVirtualFunc(); //Uses DerivedB's version.
std::vector<MyBase*> myArray; //Array of multiple different class types of the same base class.
myArray.push_back(&myBase);
myArray.push_back(&derivedA);
myArray.push_back(&derivedB);
for(int i = 0; i < myArray.size(); i++)
{
myArray[i]->MyVirtualFunc();
}
return 0;
}
Don't abuse it, but learn it, and when it seems to be the best solution for that particular case, use it. It's just another tool in your toolbox, but don't use it for every situation you encounter.
For general guidelines: [
Single Responsibillity Principle] [
Is-a vs
Has-a]
Also: [
Inheritance - Basics] and [
Inheritance - Virtual functions], and
all the rest of that site.
Edited by Servant of the Lord, 06 July 2012 - 11:54 AM.