C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 16th, 2005, 09:56 PM
marmo marmo is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 37 marmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 20 sec
Reputation Power: 5
Inheritance and copy constructors

The problem I am having is when I make a class that contains a pointer to a base class. Anytime you have memory that is dynamically allocated you must write your own copy constructors, but how do you copy a pointer if you don't know what child class has been used to allocate the memory. The following code should help put my problem into perspective. All definitions have been excluded aside from the Garage class assignment operator, which is where the problem area lies.

Code:
   // Base class Vehicle
   class Vehicle
   {
   	public:
   		Vehicle();
   		virtual Vehicle()=0 { }
   };
   
   
   // Class car inherits from Vehicle
   class Car : public Vehicle
   {
   	public:
   		Car();
   		~Car();
   };
   
   
   // Class Truck inherits from Vehicle
   class Truck : public Vehicle
   {
   	public:
   		Truck();
   		~Truck();
   };
   
   
   // Class Garage contains a pointer to a Vehicle
   class Garage
   {
   	public:
   		Garage
   		Garage( const Garage rhv );
   		Garage
   		Garage& operator=( const Garage rhv );
   
   
   		void 	 SetVehicle( Vehicle* pVehicle );
   		Vehicle* GetVehicle() const;
   
   
   	private:
   		Vehicle* m_pVehicle;
   };
   
   // Class Garage assignment operator
   Garage& Garage::operator=( const Garage rhv )
   {
   	if( this != &rhv )
   	{
   		// Copy the vehicle pointer
   		if( !m_pVehicle )
   		{
   			// Check if new memory needs to be allocated
   			if( rhv.m_pVehicle )
   			{
 		 		 // The problem lies here when I want to allocate the new memory
 				// I dont know whether to allocate a truck or a car.
   
   				//m_pVehicle = new Car;
   				//m_pVehicle = new Truck;
   				*m_pVehicle = *rhv.m_pVehicle;
   			}
   		}
   		else
   		{
   			// Check if the vehicle should be removed
   			if( rhv.m_pVehicle )
   				*m_pVehicle = *rhv.m_pVehicle;
   			else
   				RemoveVehicle();
   		}
   	}
   	return *this;
   }
   
   
   
   int main()
   {
   	Garage myGarage;
   	Car* pCar = new Car;
   	myGarage.SetVehicle( pCar );
   
   
   	Garage yourGarage;
   	yourGarage = myGarage;
   	
   	return 0;
   }
   


As you can probably see in the above code, the child type is unknown when trying to assign one Vehicle pointer to another. Now I know what your probably thinking, Runtime Type ID, or use a string member to hold the class type ie "Car" but I am curious to know if there is a more elegant solution to this problem. Let me know what ya think, for now I will just use a string member.

Reply With Quote
  #2  
Old March 2nd, 2005, 02:37 PM
KMan KMan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 1 KMan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 56 sec
Reputation Power: 0
Have you tried using the templates? If you don't want to use RTTI or a flag then the only option I can think of is templatizing the functions where you will not have to refer to any specific objects for instantiation. You might want to give it a try with that approach.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Inheritance and copy constructors


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT