• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

Copying a CRecordSet object to another CRecordSet Object

If I have the following class defined:
 class CTestSet : public CRecordset

Using the below code, how would I copy what is in the object "pOldTestSet" into "pNewTestSet" without copying each variable one by one?  
 CTestSet *pOldTestSet;
 CTestSet pNewTestSet;

 //code to populate pOldTestSet with database info
 //code to copy what is in Old into New
 //I have tried the code below
 *pNewTestSet = *pOldTestSet; // but this does not work
1 Solution
Well it does not work because the CRecodset does not have a predefined operator = . You can make a copy of the recordset using a snapshot of the memory with a code like that :
memcpy ((void*) pNewTestSet, (void*) pOldTestSet, sizeof(CTestSet));

but this will cause many potential problems with the lifetime of the objects. Because when you destroy pOldTestSet its members will free the memory allocated so leaving the pointers inside pNewTestSet incorrect.

Try consider not making a copy of the recordset. Can't you just pass pointers to the real object and make copies of the pointers instead of the object. When you need to remember the position simply use bookmarks!
You can't copy CRecordset objects; the class contains too much state, and also includes state which is external to its implementation. The class doesn't provide an operator= or a copy constructor for exactly this reason.

The class includes a handle to an ODBC query, which can't be duplicated. The class has bound its data members to the query, and the binding addresses can not be changed.

If you want, you can create a new instance of a CRecordset-derived class and _not_ call Open() on it. Then, you can copy the data members from another instance of the class (whether it is open or not) to your new instance of the class. That might be a convenient way to carry a particular record around, but that new recordset won't be connected to the database.

To do that kind of copy, you'd need to perform a member-by-member copy yourself. You could do that as an implementation of operator= or a copy constructor, yourself.

Before embarking on this mission, it's imperative that you understand object copying semantics--and from the nature of your question, I'm not sure you do.

B ekiM

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now