Solved

Copying a CRecordSet object to another CRecordSet Object

Posted on 1998-09-24
2
431 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 100 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now