473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Operator New help

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.

Mar 29 '07 #1
8 1982

the code in the OP has a number of inconsistantcies.

john wrote:
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...
Mar 29 '07 #2
Gianni Mariani wrote:
>
the code in the OP has a number of inconsistantcies.
>>
---------------- 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()
> {
}
Why is that better? Granted they do the same thing, but the 0 seems
more transparent.
Mar 30 '07 #3

"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:jt*****************@newssvr23.news.prodigy.ne t...
Gianni Mariani wrote:
>>
the code in the OP has a number of inconsistantcies.
>>>
---------------- 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()
>> {
}

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
Mar 30 '07 #4
#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??

Mar 30 '07 #5
On Mar 29, 4:12 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
the code in the OP has a number of inconsistantcies.

john wrote:
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...- 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.

Mar 30 '07 #6
Mike Wahler wrote:
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:jt*****************@newssvr23.news.prodigy.ne t...
>Gianni Mariani wrote:
>>the code in the OP has a number of inconsistantcies.

---------------- 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()

{
}
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.
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.
Mar 30 '07 #7
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.
Mar 30 '07 #8
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::operator =(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::operator =(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;
}
Mar 30 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
3
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
6
by: idikoko | last post by:
I need help overloafing the <,> operators! I wrote a very simple complex numbers class which basically treats a double as an imaginary number. I wrote the code to overload the >,< operators but...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
11
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
6
by: edd | last post by:
Hello all, Is there a way to determine whether a particular type supports the -> operator at compile time? I'm trying to write a template function (or a series of overloads) that will yield the...
17
by: Corey Cooper | last post by:
I have a class for which I needed to create an operator= function. I then use that as a base class for another class, which also needs an operator=. What is the syntax to use in the derived class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.