?
Solved

Copying a CRecordSet object to another CRecordSet Object

Posted on 1998-09-24
2
Medium Priority
?
478 Views
Last Modified: 2013-11-19
If I have the following class defined:
//header
 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?  
//cpp
MyProgram::Copy()
{
 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
0
Comment
Question by:asavino
2 Comments
 

Expert Comment

by:bobbym
ID: 1322609
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!
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 300 total points
ID: 1322610
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


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question