5 Replies - 628 Views - Last Post: 06 August 2012 - 07:05 PM Rate Topic: -----

#1 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 23-April 12

Deep copy beginner

Posted 06 August 2012 - 05:48 PM

Im working on a homework assignment to implement an array of wheel objects in a heap and to write a deep copy constrcutor for the class RacingCar. I think i have the heap classes working but i'm not sure if the constructor are creating deep copies. I have followed an example in my lecture notes but when i try to use the -> operator on line 83 (commented line) i get an error saying: base operand of '->' has non-pointer type 'RacingCar'

I'm new to heap memory and C++ copy constructors

Full code:
#include <iostream>
#include <conio.h>

using namespace std;
//---------------------------------------------------------------------------
#include <iostream>
#include <conio.h>

using namespace std;
//---------------------------------------------------------------------------
class Wheel
{
	public: Wheel() : pressure(32)
			{
				ptrSize = new int(30);
			}

			Wheel(int s, int p) : pressure(p)
			{
				ptrSize = new int(s);
			}

			~Wheel() { delete ptrSize; }

			void pump(int amount)
			{
				pressure += amount;
			}

	private:
		   int *ptrSize;
		   int pressure;
};
//---------------------------------------------------------------------------
class RacingCar
{
	public:
		   RacingCar()
		   {
			   speed = 0;

			   for (int i = 0; i < 4; i++)
				  wheels[i] = new Wheel();
		   }

		   //copy constructor
		   RacingCar(const RacingCar &oldCar)
		   {
               for (int i = 0; i < 4; i++)
				  wheels[i] = new Wheel();
			   
			   speed = oldCar.speed;
		   }

		   RacingCar(int value)
		   {
			   speed = value;

			   for (int i = 0; i < 4; i++)
				  wheels[i] = new Wheel();
		   }

		   ~RacingCar() { delete [] wheels; }

		   void accelerate(int value) { speed = speed + value; }

		   void print() { cout << speed; }

	private:
		   int speed;
		   Wheel *wheels[4];
};
//---------------------------------------------------------------------------
int main()
{

	RacingCar *ptrCar = new RacingCar(10);
	RacingCar car2 = *ptrCar;
	
	ptrCar->accelerate(60);
	ptrCar->print();
	
	//car2->accelerate(50);
	car2.accelerate(40);
	
	delete ptrCar;
	getch();
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Deep copy beginner

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,003
  • Joined: 08-August 08

Re: Deep copy beginner

Posted 06 August 2012 - 05:57 PM

That's because car2 is not a pointer to an object, it's an instance of it copied from the object at the address pointed to by *ptrCar.
Was This Post Helpful? 1
  • +
  • -

#3 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 23-April 12

Re: Deep copy beginner

Posted 06 August 2012 - 06:31 PM

View PostCTphpnwb, on 06 August 2012 - 05:57 PM, said:

That's because car2 is not a pointer to an object, it's an instance of it copied from the object at the address pointed to by *ptrCar.


Thanks, can you tell me if my copy constructor is creating deep copies? i have to prove it somehow and i have no idea how
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3456
  • View blog
  • Posts: 10,661
  • Joined: 05-May 12

Re: Deep copy beginner

Posted 06 August 2012 - 06:36 PM

Tire pressures are not being copied.

Can't test or show that though since you have no code to access the tires.
Was This Post Helpful? 1
  • +
  • -

#5 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 23-April 12

Re: Deep copy beginner

Posted 06 August 2012 - 06:49 PM

View PostSkydiver, on 06 August 2012 - 06:36 PM, said:

Tire pressures are not being copied.

Can't test or show that though since you have no code to access the tires.


We were given the code for the Wheels class to implement so i'm not sure if we're allowed to modify it. But if i change the pressure member to public i can access it in the RacingCar copy constructor... does the following code also copy the pressure from wheels?

		   RacingCar(const RacingCar &oldCar)
		   {
               for (int i = 0; i < 4; i++)
               {
				  wheels[i] = new Wheel();
				  wheels[i]->pressure; //does this copy the pressure?
               }
			   
			   speed = oldCar.speed;
		   }



Thanks
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3456
  • View blog
  • Posts: 10,661
  • Joined: 05-May 12

Re: Deep copy beginner

Posted 06 August 2012 - 07:05 PM

No, line 5 does not copy the pressure.

The correct way to copy the wheel is to pass the oldCar's wheels one at a time to the Wheel copy constructor on line 5.

Also, I wouldn't make pressure public. It would be better to add a public method name getPressure() that returns the tire pressure.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1