Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting base class behaviour from inherited class 1

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
I have two classes, the second inheriting from the first, like follows:

class Tuu_file
{
public:
Tuu_file(){}
virtual ~Tuu_file(){}

virtual void Function(){
cout<<&quot;Tuu_file::Function() run.&quot;;}
};

class Tuu_complex_file:public Tuu_file
{
public:
Tuu_complex_file(){}
virtual ~Tuu_complex_file(){}

virtual void Function(){
cout<<&quot;Tuu_complex_file::Function() run.&quot;;}
};

I place a series of Tuu_complex_file objects into a list<Tuu_file>.
When I iterate through the objects in the list & call Function() on them, I would expect to see the output from Tuu_complex_file::Function(). Instead I get the output from Tuu_file::Function.

Does anyone know where I'm going wrong?
Would making my list a list to Tuu_file pointers allow me to use the inherited function?

Cheers,
Douglas JL Common sense is what tells you the world is flat.
 
Since it is a list of Tuu_file, it will always pick the Tuu_file::Function. Change the list to

list<Tuu_file*>

and access it from the pointer. eg

typedef list<Tuu_file*> Tuu_list;
typedef list<Tuu_file*>::iterator Tuu_iter;
...
Tuu_list theList;
...
for (Tuu_iter iter = theList.begin (); iter != theList.end (); ++iter)
{
(*iter)->Function ();
}

 
That did the trick, cheers. Common sense is what tells you the world is flat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top