Advertisement

virtual copy constructors

Started by January 04, 2003 01:03 AM
2 comments, last by deal 21 years, 10 months ago
just when i thought i understood..........
  


#include <iostream>
using namespace std;

class Mammal
{
public:
	Mammal():itsAge(0), itsWeight(0) {}
	virtual ~Mammal() {}
	Mammal(const Mammal &rhs);
	virtual Mammal * Clone()
		{return new Mammal(*this);}
	int GetAge()const
		{return itsAge;}
	void SetAge(int age)
		{itsAge = age;}
	int GetWeight()const
		{return itsWeight;}
	void SetWeight(int weight)
		{itsWeight = weight;}
protected:
	int itsAge;
	int itsWeight;
};

Mammal::Mammal (const Mammal & rhs):
itsAge(rhs.GetAge()),
itsWeight(rhs.GetWeight())
{
}

class Dog : public Mammal
{
public:
	Dog() {}
	virtual ~Dog() {}
	Dog (const Dog & rhs);
	virtual Mammal * Clone()
		{return new Dog(*this);}
};

Dog::Dog(const Dog & rhs):Mammal(rhs)
{
}

//-------------------------

int main()
{
	Dog *Lucky = new Dog;
	Mammal *Meg = Lucky->Clone(); // this is fine

	//Dog *Meg = Lucky->Clone();  // cannot convert from ''class Mammal *'' to ''class Dog *''


	return 0;
}

  
can someone explain what is preventing Dog *Meg = Lucky->Clone(); from working ? and yes lucky and meg are my dogs
Clone is declared as returning a Mammal*, so even though you know that it in this case returns a Dog*, you need to manually cast to the actual type, like this:

  Dog* meg = static_cast<Dog*>(Lucky.Clone())  


However, IIRC, C++ does support covariant returns, so you can do this in the Dog class:

  virtual Dog* Clone(){...}  

I''m 95% sure that covariant returns are in the standard - whether it is supported by compilers is another matter - I have never tried it.


I''ve looked at the source, and there are pieces that are good and pieces that are not ... My experience and some of my friends'' experience is that Linux is quite unreliable. Microsoft is really unreliable but Linux is worse. -- Ken Thompson
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Advertisement

  Dog* meg = static_cast<Dog*>(Lucky.Clone())  


wrong operator ?


  Dog* meg = static_cast<Dog*>(Lucky->Clone())  


thanx problem solved.

btw Visual C++ 6 does not support covariant returns.

which sucks because that makes my code look so bloody ugly

[edited by - deal on January 4, 2003 3:28:49 AM]
Well you shouldn't use casting since that isn't very good form. You can try this example I made for you that uses mammals and dogs.

#include < iostream >

using namespace std;

class Mammal
{
public:
Mammal();
virtual ~Mammal(){};
void setAgeAndWeight(int age,int weight)
{
itsAge=age;
itsWeight=weight;
}

int getAge() {return itsAge;}
int getWeight() {return itsWeight;}
virtual Mammal *clone()
{
return new Mammal(*this);
}
private:
int itsAge;
int itsWeight;
};

Mammal::Mammal():
itsAge(0),itsWeight(0)
{
}

class Dog: public Mammal
{
public:
Dog():Mammal(){};
~Dog(){};
virtual Mammal *clone()
{
Dog *pDog=new Dog;
*pDog=*this;
return pDog;
}
};

int main()
{
Mammal *pLucky=new Dog;

pLucky->setAgeAndWeight(5,5);

Mammal *Megs;

Megs=pLucky->clone();

return 0;
}


[edited by - LordByron on January 4, 2003 8:22:22 AM]

[edited by - LordByron on January 4, 2003 8:23:17 AM]

[edited by - LordByron on January 4, 2003 8:24:24 AM]

[edited by - LordByron on January 4, 2003 8:26:18 AM]

This topic is closed to new replies.

Advertisement