By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

overloading << and inheritance

P: n/a
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;
}

Mar 12 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ook wrote:
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";}
};
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;
}
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 12 '07 #2

P: n/a
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;
}

Mar 12 '07 #3

P: n/a
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.

Mar 12 '07 #4

P: n/a
Ook wrote:
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.

--
Salu2
Mar 12 '07 #5

P: n/a
Ook
On Mar 12, 1:07 pm, Julián Albo <JULIANA...@terra.eswrote:
Ook wrote:
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.

--
Salu2
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.

Mar 12 '07 #6

P: n/a
Ook wrote:
On Mar 12, 1:07 pm, Julián Albo <JULIANA...@terra.eswrote:
>Ook wrote:
>>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.

--
Salu2

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.
Mar 12 '07 #7

P: n/a
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 :)

Mar 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.