473,507 Members | 12,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deep copy constructor needed for an class array within another class

4 New Member
Hi,
I need a deep copy constructor for a class which contains an array for another class:

class Chessboard
{
public:
ChessSquare chessSquare[64];
// copy constructor needed to copy all chessSquare and attributes

}

class ChessSquare
{
public:
int column;
int row;

}

void main (void)
{

Chessboard a1;
Chessboard a2 = a1; //deep copy needed. any suggestions for a deep copy constructor?
}

Any one can post something that can help this case? Thanks.
Nov 23 '06 #1
3 6563
sivadhas2006
142 New Member
Hi,

I think this is one way to implement the copy constructor.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #define NO_OF_SQUARES   64
  3.  
  4. class ChessSquare
  5. {
  6.    public:
  7.       int m_nColumn;
  8.       int m_nRow;
  9. };
  10.  
  11. class ChessBoard
  12. {
  13.    private:
  14.  
  15.       ChessSquare m_objChessSquare[NO_OF_SQUARES];
  16.  
  17.    public:
  18.  
  19.       ChessBoard(const ChessBoard &a_objChessBoard)
  20.       {
  21.          for(int i = 0; i < NO_OF_SQUARES; i++)
  22.          {
  23.             m_objChessSquare[i] = a_objChessBoard.m_objChessSquare[i];
  24.          }           
  25.    }  
  26. };
  27.  
  28.  
Regards,
M.Sivadhas.
Nov 23 '06 #2
unicorn7777777
4 New Member
ok then there's another problem.
What if i want to copy values of a chessboard that is a pointer to a value that is a normal Chessboard?
For example.

line 1: Chessboard *pBoard = CurrBoard;

line 2: Chessboard c2 = (*pBoard); // i need a deep copy of Currboard into c2..... will this work?


I know c2 = CurrBoard will work, but for some reason, when I pass a pointer through a function and try to copy it like above, it doesnt work. I get rubbish values:
*pBoard = Currboard;
AI deepblue.update(*pBoard);

Class AI
{
Chessboard myboard;
}
void AI::update(Chessboard c)
{
this->myboard = c;
}

Nor does this work:
*pBoard = Currboard
AI deepblue.update(pBoard);

Class AI
{
Chessboard myboard;
}
void AI::update(Chessboard *c)
{
this->myboard = (*c);
}

and that is with the copy constructor you have advised me as above. Do i need another copy constructor for passing pointers or something? please advise.
Thanks
Nov 23 '06 #3
sivadhas2006
142 New Member
Hi,

Well,
Can you post your full code?

Regards,
M.Sivadhas.
Nov 23 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

42
5720
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
5
3262
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
4
1998
by: lars.uffmann | last post by:
Hey everyone! I am (still) working on a project that I took over from former students, so don't blame me for the criminal approach on coding *g* The problem I have is fairly easy and while I...
5
1983
by: pauldepstein | last post by:
I recently had a job interview question which I totally failed. (The question seemed excellent from an objective point of view, but having completely failed to do it, my subjective feelings are...
11
3407
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
13
4986
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
1
2106
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a...
3
3373
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
3
8738
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
0
7314
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,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.