6 Replies - 4379 Views - Last Post: 06 May 2007 - 10:24 AM Rate Topic: -----

Topic Sponsor:

#1 Pontus  Icon User is offline

  • Cattlebruiser
  • member icon

Reputation: 15
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Vector of derived classes

Posted 05 May 2007 - 05:51 AM

Hi. I was trying to put a dervided class into a vector that is meant to hold the base class:
class creature()
public:
void greet(){
cout<<"Hello"<<endl;
}
};
class human : public creature{
public:
void greet(){
cout<<"Hello, i am a human"<<endl;
}
}

vector<creature> creatures;
human human1;
creatures.push_back(human1);
creatures[0].greet();

The output is then:
Hello 

It should be: Hello, i am a human
What is the solution?

Greetzzz
Hyperion

This post has been edited by manhaeve5: 05 May 2007 - 05:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Vector of derived classes

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2140
  • View blog
  • Posts: 9,138
  • Joined: 18-February 07

Re: Vector of derived classes

Posted 05 May 2007 - 07:38 PM

Well theortically this was an easy question as you did not make the function a virtual function. Virtual functions allow for dynamic binding durring run time. However when I tried it myself I still got the creatures function. Apperently when it goes into the vector all knowledge of its type is thown away. Even casting a pointer to it does not fix the problem. I had to actually tell it spacificly to run the human's function.

So what do we do? Well the following code works:
#include <vector>
#include <iostream>
using namespace std;

class creature
{
public:
	virtual void greet() //<--Note the "virtual"
	{
		cout<<"Hello"<<endl;
	}
};

class human : public creature
{
	public:
	void greet()
	{
		cout<<"Hello, I am a human!"<<endl;
	}
};

int main()
{

	vector<creature*> creatures;
	human human1;
	creatures.push_back(&human1);

	creatures[0]->greet(); //prints, "Hello, I am a human!"
	return 0;
}
as it only askes the class to store a pointer to the object. This way the type is not currupted by the STL class. I think this is a better way of doing things myself anyway.

Here is an artcle on the matter: STL and OO Don't Easily Mix <-- interesting read.
Was This Post Helpful? 0
  • +
  • -

#3 Pontus  Icon User is offline

  • Cattlebruiser
  • member icon

Reputation: 15
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Vector of derived classes

Posted 05 May 2007 - 10:42 PM

Thx for the reply. Too bad it doesnt work
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2140
  • View blog
  • Posts: 9,138
  • Joined: 18-February 07

Re: Vector of derived classes

Posted 06 May 2007 - 08:32 AM

View Postmanhaeve5, on 5 May, 2007 - 10:42 PM, said:

Thx for the reply. Too bad it doesnt work


The above program didn't work for you?

Or do you mean the idea of storing derived classes in a vector? Truth is, all you really want to store in the vector is a pointer to the object anyway. You don't want to waste a lot of time copying objects when you don't need to.
Was This Post Helpful? 0
  • +
  • -

#5 Pontus  Icon User is offline

  • Cattlebruiser
  • member icon

Reputation: 15
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Vector of derived classes

Posted 06 May 2007 - 08:45 AM

I mean storing a derived class in a vector
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2140
  • View blog
  • Posts: 9,138
  • Joined: 18-February 07

Re: Vector of derived classes

Posted 06 May 2007 - 09:20 AM

it is rather a shame. I have to say that this is one area where Java really outshines C++. On the other hand, java does almost everythign with pointers so really it is just up to you to do it right.

The problem with storing abstract data types in a container is that it is hard to know how to copy the data. If you told the compiler you are using datatype X then it can generate code to copy X, but it does not know all the details of derived classes you might add in the future so it does not know how to generate code to copy Y:X. Sure you can write a copy method, and copy constructor etc etc, but this still does not solve the compilers problem because it does not know anything about the derived class.

now if you KNEW that all derived classes had a copy constructor, you could create an abstract container class that could use polymorphic data types so long as they conformed to an interface (i.e. had a copy constructor).
Was This Post Helpful? 0
  • +
  • -

#7 Pontus  Icon User is offline

  • Cattlebruiser
  • member icon

Reputation: 15
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Vector of derived classes

Posted 06 May 2007 - 10:24 AM

I dont know what you mean but thx anayway
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1