Operator New help

J

john

Hey guys,

Quick question i have this code and what i want to do is create a deep
copy of class B. Now I tried doing this with the new operator and
pointers. Here's is the orignal


---------------- Original Copy-----------------------


#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 = &x; // Here is the problem so I implemented a new
command


}

void print ()
{
a->print();
}
B(const B& copy){ // Class B copy constructor
a = copy.a;

}
const B &operator=(const B x){
a = x.a; // Operator
}
B::~B(){
delete a;



}
};


int main()
{

A a(5);
A a1(7);


a1.print();

B b(a1);
b.print();
B c(a);
b.print();
b = c;

b.print();
cout << endl;
int trick;
cin >> trick;
return 0;
}

//--------------------End of Orignal Copy-----------------------


----Version altered with new command in class B--------


#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); //New command


}

void print ()
{
a->print();
}
B(const B& copy){ // Class B copy constructor
a = copy.a;

}
const B &operator=(const B x){
a = x.a; // Operator
}
B::~B(){
delete a;



}
};


int main()
{

A a(5);
A a1(7);


a1.print();

B b(a1);
b.print();
B c(a);
b.print();
b = c;

b.print();
cout << endl;
int trick;
cin >> trick;
return 0;
}

//---------------------------------------------------------------------
Works fine but say if i change main() so it looks like this

------------------ Aletered main -----------------

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;
}


--------------------- End of altered main--------------------

I get a bunch of junk so I found out i need to make a deep copy of B.

This is what i came up with

int *p;
p = new int();
*p = a;

I came up with invalid conversion from 'A*' to 'int'. I don't get why
this wouldn't work.


thank you


john


any help would be appreciated.
 
G

Gianni Mariani

the code in the OP has a number of inconsistantcies.
Hey guys,

Quick question i have this code and what i want to do is create a deep
copy of class B. Now I tried doing this with the new operator and
pointers. Here's is the orignal


---------------- Original Copy-----------------------


#include <iostream>
using namespace std;

class A
{
int n ;
public:
A():n(0)

The line above is valid - this is better:

A():n()
{
}
A(int x):n(x)
{
n = x;

why make an assignment to what you already initialized it to ?
}

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

Why provide your own copy constructor ? Is not the one provided by the
compiler good enough here ?
A(const A& objectCopy){

n = objectCopy.n; // copy constructor

}


};

class B
{
A * a;

public:

B(A & x)
{
a = &x; // Here is the problem so I implemented a new
command


}

void print ()
{
a->print();
}
B(const B& copy){ // Class B copy constructor
a = copy.a;

the line above does not make a copy of the object.
}
const B &operator=(const B x){
a = x.a; // Operator
}
B::~B(){
delete a;

if you delete - make sure you have a correspoinding new.
}
};


int main()
{

A a(5);
A a1(7);


a1.print();

B b(a1);
b.print();
B c(a);
b.print();
b = c;

b.print();
cout << endl;
int trick;
cin >> trick;
return 0;
}

//--------------------End of Orignal Copy-----------------------


----Version altered with new command in class B--------


#include <iostream>
using namespace std;

class A
{ .... see earlier commens
};

class B
{
A * a;

public:

B(A & x)

B( const A & x )
{
a = new A(x); //New command
better



}

void print ()
{
a->print();
}
B(const B& copy){ // Class B copy constructor
a = copy.a;

// try this one
B(const B& copy) a( new A(* copy.a) ){}
}
const B &operator=(const B x){
a = x.a; // Operator
delete a;
a = new A(* copy.a);
}
B::~B(){
delete a;



}
};


int main()
{

A a(5);
A a1(7);


a1.print();

B b(a1);
b.print();
B c(a);
b.print();
b = c;

b.print();
cout << endl;
int trick;
cin >> trick;
return 0;
}

//---------------------------------------------------------------------
Works fine but say if i change main() so it looks like this

------------------ Aletered main -----------------

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;
}


--------------------- End of altered main--------------------

I get a bunch of junk so I found out i need to make a deep copy of B.

That's expected....
....
any help would be appreciated.

Hope it helps...
 
M

Mark P

Gianni said:
the code in the OP has a number of inconsistantcies.


The line above is valid - this is better:

A():n()

Why is that better? Granted they do the same thing, but the 0 seems
more transparent.
 
M

Mike Wahler

Mark P said:
Why is that better? Granted they do the same thing, but the 0 seems more
transparent.

If the type of 'n' is later changed to some other
type where 0 is not a valid initializer, the syntax
n() will still cause 'n' to be default constructed,
and in the case of scalar types, still initialized to zero.

-Mike
 
J

john

#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);
// x.operator=(x);

}

void print ()
{
a->print();
}




// try this one
B(const B& copy) a( new A(* copy.a) ){


}
const B &operator=(const B x){
a = x.a; // Operator



delete a;
a = new A(* copy.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;
}

---------------------------------------
I changes the code to the following, and their is a parse error?

in the line
B(const B& copy) a( new A(* copy.a) )

B and a are both making a copy??
 
J

john

the code in the OP has a number of inconsistantcies.










The line above is valid - this is better:

A():n()


why make an assignment to what you already initialized it to ?



Why provide your own copy constructor ? Is not the one provided by the
compiler good enough here ?







the line above does not make a copy of the object.




if you delete - make sure you have a correspoinding new.















... see earlier commens



B( const A & x )


// try this one
B(const B& copy) a( new A(* copy.a) ){}




delete a;
a = new A(* copy.a);
















That's expected....
...


Hope it helps...- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

hey Gianni,

This code was given to me as trying to understand classes better. I
looked in the book and tried to make a new pointer to *a but falied
several times before I posted this message.
 
M

Mark P

Mike said:
If the type of 'n' is later changed to some other
type where 0 is not a valid initializer, the syntax
n() will still cause 'n' to be default constructed,
and in the case of scalar types, still initialized to zero.

True-- and yours is the answer I was anticipating-- but if you're
modifying something as important as the data type of a member, a warning
from the compiler to recheck your constructor doesn't seem like such a
bad thing.
 
G

Gianni Mariani

Mark P wrote:
....
True-- and yours is the answer I was anticipating-- but if you're
modifying something as important as the data type of a member, a warning
from the compiler to recheck your constructor doesn't seem like such a
bad thing.

There are only so many ways that you can default construct a scalar.
 
G

Gianni Mariani

john wrote:
.....
---------------------------------------
I changes the code to the following, and their is a parse error?

in the line
B(const B& copy) a( new A(* copy.a) )

B and a are both making a copy??

OK - this should give you a better idea - when playing/learning about
C++ objects it's usually good to output a message at the constructor and
destructor just to get a good idea of what happens when.

I compiled this below so it should compile on your system as well

#include <string>
#include <iostream>
using namespace std;

const bool doecho = true;

template <typename T>
void Echo( T * obj, const std::string & str )
{
if ( doecho )
{
cerr << "Echo (" << obj << ") - " << str << "\n";
}
}

class A
{
int n;

public:

A()
: n()
{
Echo( this, "Default construct A" );
}

A( int x )
: n(x)
{
Echo( this, "Construct A(int)" );
}

// A( const A& ) ... normally use default copy constructor
A( const A & ia )
: n( ia.n )
{
Echo( this, "Construct A( const A& )" );
}

~A()
{
Echo( this, "~A()" );
}

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

};

class B
{

A * a;

public:

// all constructors should new an A

B(const A & x)
: a( new A(x) ) // initialize with an A - make a new A here
{
Echo( this, "Construct B(const A&)" );
}

B(const B & copy)
: a( new A(* copy.a) ) // deep copy the A
{
Echo( this, "Construct B(const B&)" );
}

B & operator=(const B & x)
{
Echo( this, "B::eek:perator =(const B&) - start" );

// need to make a new A from the B coming in
// and we need to delete the A that is currently
// being pointed to


// make this thread safe
// new can throw so make it so things can recover
A * pa( a );

// the "new" A pointer replaces the old A pointer
a = new A( * x.a );

// we didn't throw - delete the old A object
delete pa;

Echo( this, "B::eek:perator =(const B&) - end" );

// assignment should always return one-self
return *this;
}

// the destructor should delete the A new()ed in the
// constructor or the assignment operator
~B()
{
Echo( this, "~B() - start" );
delete a;
Echo( this, "~B() - end" );
}

void print ()
{
a->print();
}

};

//---------------------------------------------------------------------------
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;
}
 

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

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top