Please have a look at the following code
Location.h
#pragma once
class Location
{
public:
Location(void);
Location(int,int,int);
~Location(void);
Location(const Location *loc);
void display();
void set(int,int,int);
private:
int x,y,z;
};
Location.cpp
#include "Location.h"
#include <iostream>
#include <string>
using namespace std;
Location::Location()
{
}
Location::Location(int x,int y, int z)
{
set(x,y,z);
}
Location::~Location(void)
{
}
void Location::display()
{
cout << "X: " << x << endl;
cout << "Y: " << y << endl;
cout << "Z: " << z << endl;
}
void Location::set(int xx,int yy,int zz)
{
x = xx;
y = yy;
z = zz;
}
Object.h
#pragma once
#include "Location.h"
class GameObject
{
public:
GameObject(int);
~GameObject(void);
GameObject(int,Location *);
GameObject(const GameObject *obj);
Location *location;
int getNumberOfObjects();
static int counter;
int id;
private:
GameObject(void);
};
Object.cpp
#include "GameObject.h"
#include <iostream>
using namespace std;
static int counter = 0;
int GameObject::counter = 0;
GameObject::GameObject(void)
{
}
GameObject::GameObject(int i)
{
counter++;
id = i;
}
GameObject::GameObject(int i,Location *loc)
{
id = i;
location = loc;
}
GameObject::GameObject(const GameObject *ob)
{
this->location = new Location(ob->location);
}
GameObject::~GameObject(void)
{
}
Main.cpp
#include <iostream>
#include "GameObject.h"
using namespace std;
int main()
{
//GameObject obj1;
//cout << obj1.id << endl;
GameObject obj2(45);
cout << obj2.id << endl;;
// cout << obj2.counter << endl;
//Declaring dynamic objects for Location
Location *loc1 = new Location(1,1,1);
Location *loc2 = new Location(2,2,2);
Location *loc3 = new Location(3,3,3);
//Assigning each GameObject a location
GameObject obj3(45,loc1);
GameObject obj4(45,loc2);
GameObject obj5(45,loc3);
//Displaying Number of GameObject objects
cout << "Number of Objects: " << GameObject::counter << endl;
//Invoking Location's display() method using GameObject objects
cout << "......obj3 values........" << endl;
obj3.location->display();
cout << "......obj4 values........" << endl;
obj4.location->display();
cout << "......obj5 values........" << endl;
obj5.location->display();
//Declaring new Static GameObject
GameObject obj6(obj4);
//Invoking display() member function using both obj4 and obj6 GameObjects
cout << endl;
cout << "Invoking display() member function using both obj4 and obj6 GameObjects " << endl;
obj4.location->display();
obj6.location->display();
//Changing the location values in obj4
obj4.location->set(8,8,8);
//Invoking display() member function using both obj4 and obj6 GameObjects
cout << endl;
cout << "Invoking display() member function using both obj4 and obj6 GameObjects " << endl;
obj4.location->display();
obj6.location->display();
system("pause");
return 0;
}
In here, as you can see in Main.cpp, obj6 ‘Location’ has been changed as soon as obj4 ‘Location’ is changed. I don’t want to change the obj6 ‘Location’ when I change the location for obj4.
I get this error when this is executed
GameObject.obj : error LNK2019: unresolved external symbol "public: __thiscall Location::Location(class Location const *)" (??0Location@@QAE@PBV0@@Z) referenced in function "public: __thiscall GameObject::GameObject(class GameObject const *)" (??0GameObject@@QAE@PBV0@@Z)
I tried to do it with a deep copy constructor, but it didn’t work. Please help.
is not the copy constructor, You need:
Also, you need to provide a copy assignment operator which does a deep copy.
Note that the ideal solution is to use smart pointer as member instead of an raw pointer.
This shall basically save you all the hassles of deep copying.
Good Read:
What is The Rule of Three?