Copy Constructor
Hey guys,
Thank you for your input from the last topic. I tried referencing my c+
+ book and googled the topic but i'am still confused a bit. I have
another problem with that same code. I was hinted what i needed all
along was a copy constructor for class A.
Class B contains a = new A(x) in it's constructor. This is good but
this doesn't make sense. Does that really fit the specs of either
construtors of class A?? So i was brought into the area of making a
copy constructor for class A. This is where i'am having problems i
made one that creates a copy of n but i cannot of x it does not
consider x a member of class A. Do i need to use pointer this?? Code
below.
#include <iostream>
using namespace std;
class A
{
int n ;
public:
A():n(0)
{
}
A(int x):n(x)
{
n = x;
}
void print()
{ cout << n<<"\n\n";
}
A(const A& objectCopy){
n = objectCopy.n; // copy constructor
}
};
class B
{
A * a;
public:
B(A & x)
{
a = new A(x);
}
void print ()
{
a->print();
}
B(const B& copy){ // Class B copy constructor
a = copy.a;
//int *p;
//p = new copy.a;
}
const B &operator=(const B x){
a = x.a; // Operator
}
B::~B(){
delete a;
}
};
//---------------------------------------------------------------------------
int main()
{
A a(5);
B b = a;
{
A a1 (7);
B b1 =a1;
b = b1;
}
b.print();
cout << endl;
int trick;
cin >> trick;
return 0;
}
//--------------------------------------------------------------------------------
Thanks guys,
John