Originally Posted by
7stud
I can't make any sense of that. Why don't you describe a simple numerical example of what you want to do?
Same example as before.
Code:
object1
{
public:
object1(void)
{cout << "This is object1, the base class. I do not want this to be be output when you are creating an object2.\n";}
};
object2
{
public:
object2(void)
{cout << "This is object2, the derived class. I don't want the output from object1 to be displayed when creating this object, rather, I want only this output here displayed.";}
};
And then, since you said I cannot make object2 ignore object1:bject1(), my question was about having a seperate initializing function that is overridden for object2, like the following.
Code:
object1
{
protected:
create_me(void)
{cout << "This is object1, only to display for base objects, never for the derived object.";}
//I don't want the above output line to display when making something of class object2
object1(void){create_me();}
};
object2
{
protected:
create_me(void) //Let's override create_me so that it does something else
{cout << "This is object2, derived from object1.";}
};
You don't even need to mess with object2's constructor at that point, because the base class' constructor calls create_me(), which contains what you want it to do, which is overridden for object2 so that object1::create_me() is ignored for object2.
However, Sebastiani said that this won't work either, as object2 will have object1::create_me() called for it anyway since it is object1's constructor that is calling it.
Either way, I have come up with a way to do it based on what you guys have said here about overriding and overloading functions. It is a modified form of the solution Sebastiani gave. Since I can overload the constructor like that and get it to call a different version of it, I figure I'll just go that route. Here is how I am doing it now.
Code:
object1
{
public:
object1(void)
{
//do object1 stuff here
}
object1(const object1& O){}
};
object2
{
public:
object2(void): object1(*this)
{
//object2 stuff
}
};
And that works for what I'm after.
Thank you very much for guiding me to this solution. It's not nearly as complicated as I thought it would have to get.