overloading << and inheritance

O

Ook

The following code compiles and runs. In my overloaded operator<<, I
call Parent.stuff. I would expect it to call Child.stuff, since Child
is the child class, but it does not. What am I missing, and how can I
get it to call Child.stuff. Eventually I'll have different child
classes, and I need it to call stuff() from the associated child
class, not Parent.stuff.

#include <iostream>
#include <ostream>
#include <string>
using namespace std;

class Parent
{
public:
string stuff(){ return "Parent.stuff";}
};
class Child : public Parent
{
public:
string stuff(){ return "Child.stuff";}
};

ostream& operator<<(ostream& out, Parent * p)
{
// Overloaded << operator
out << p->stuff() << endl;
return out;
}
void printStuff( Parent * p ) { cout << p; }

int main()
{
Child c;
printStuff( &c );
return 0;
}
 
V

Victor Bazarov

Ook said:
The following code compiles and runs. In my overloaded operator<<, I
call Parent.stuff. I would expect it to call Child.stuff, since Child
is the child class, but it does not. What am I missing, and how can I
get it to call Child.stuff. Eventually I'll have different child
classes, and I need it to call stuff() from the associated child
class, not Parent.stuff.

#include <iostream>
#include <ostream>
#include <string>
using namespace std;

class Parent
{
public:
string stuff(){ return "Parent.stuff";}

virtual string stuff(){ return "Parent.stuff";}
 
O

Ook

virtual string stuff(){ return "Parent.stuff";}

Thanks for reply. I modified it as follows, this compiles, but
generates an error at runtime. I did it like this because my actual
code is large, and this demonstrates what I'm trying to do and
duplicates the actual error. I'm missing something somewhere between
the creation of the class, and calling a member function in the class,
I think my pointers/references are confused:

#include <iostream>
#include <ostream>
#include <string>
using namespace std;

class Parent
{
public:
virtual string stuff(){ return "Parent.stuff";}
};
class Child : public Parent
{
public:
string stuff(){ return "Child.stuff";}
};

ostream& operator<<(ostream& out, Parent * p)
{
// Overloaded << operator
out << p->stuff() << endl;
return out;
}
void printStuff( Parent * p )
{
cout << p;
}
Parent* createParent()
{
Child c;
return &c;
}
int main()
{
Parent *p;
p = createParent();

printStuff( p );
return 0;
}
 
O

Ook

PS - If I don't make the Parent.stuff function virtual, it runs, but
does not call the child class stuff function. Making the function
virtual causes it to crash.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Ook said:
Parent* createParent()
{
Child c;
return &c;
}

You are returning the address of a local object. When you use the returned
value, the object is already destroyed.
 
O

Ook

You are returning the address of a local object. When you use the returned
value, the object is already destroyed.

How do I return it so that it doesn't get destroyed? In main it's
expecting a pointer, so I need to point to memory that isn't released/
destroyed. BTW, this is for a school assignment, so I can't change
anything in main(). I can do what I want with the rest of the code.
The assignment isn't difficult, but I'm getting hung up here.
 
M

Matt Pearson

Ook said:
How do I return it so that it doesn't get destroyed? In main it's
expecting a pointer, so I need to point to memory that isn't released/
destroyed. BTW, this is for a school assignment, so I can't change
anything in main(). I can do what I want with the rest of the code.
The assignment isn't difficult, but I'm getting hung up here.

use dynamically allocated memory:

Parent* createParent()
{
return new Child;
}

however, you should really use delete on the returned pointer later.
(though if your program really is that short, the memory leak won't
matter). Otherwise, if having only one instance of a particular object
isn't a problem, you could use static variables:

Parent* createParent()
{
static Child c;
return &c;
}

since c is static, it won't get destroyed when the function returns.
 
O

Ook

use dynamically allocated memory:
Parent* createParent()
{
return new Child;

}

however, you should really use delete on the returned pointer later.
(though if your program really is that short, the memory leak won't
matter). Otherwise, if having only one instance of a particular object
isn't a problem, you could use static variables:

Parent* createParent()
{
static Child c;
return &c;

}

since c is static, it won't get destroyed when the function returns.

Sweeeeet. Thanks, that is the missing link. It now compiles and runs
as expected :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top