[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Copying a CRecordSet object to another CRecordSet Object

Posted on 1998-09-24
2
Medium Priority
?
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

656 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