Copy Constructor

J

john

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
 
J

john

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

I'am using the orginal code again
 
T

Thomas Matthews

john said:
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;
Redundant. The initializer list is accomplishing the same thing
as n = x, usually more efficient.
}

void print()
{ cout << n<<"\n\n";
}

A(const A& objectCopy){

n = objectCopy.n; // copy constructor
}
You can make use of initializer lists for copy constructors too.
A(const A& new_a)
: n(new_a.n)
{
}
};

class B
{
A * a;

public:

B(A & x)
{
a = new A(x);
}
You can make use of initializer lists here too.
B(A& x)
: a(new A(x))
{
}
[snip]

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


Thanks guys,

John


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
Z

zwylinux

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


It's a little complex when you have a pointer member in the class
I suggest you learned more about copy constructor.
 
J

john

It's a little complex when you have a pointer member in the class
I suggest you learned more about copy constructor.- Hide quoted text -

- Show quoted text -

I understand x runs out of scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top