473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

transfer class object from one pointer to another

I am facing a problem-

I am using

f1(this)

to pass a class object pointer to a function ( say f1)

f1 is defined as follows

f1 (classB *p1)
{

classB *p2 = new class (*p1);

..........

does not transfer the object pointer by the this pointer to p2 - it only
transfers the data values. I need to transfer p1 to p2 because p1 kepts
pointing to old objects and creates problem.

note: classB is the base class of the class whose object is passed to in
f1(this)

Any suggestion ?? Is copy constructor required? ( I have tried it but it
does not work for pointer objects)

If more clarifications are requirted please let me know.

Thank you
Jul 22 '05 #1
1 2159
Zhongqi Pan wrote:
I am facing a problem-

I am using

f1(this)

to pass a class object pointer to a function ( say f1)

f1 is defined as follows

f1 (classB *p1)
{

classB *p2 = new class (*p1);

.........

does not transfer the object pointer by the this pointer to p2 - it
only transfers the data values.
It creates a new dynamically allocated object that is a copy of the
object pointed to by p1 (by copy construction).
I need to transfer p1 to p2 because p1 kepts pointing to old objects
and creates problem.
What do you mean by "transfer p1 to p2"? Do you want p2 to point to the
same object as p1? Then just do:

classB* p2 = p1;

or do you want p2 to point to a new object that is a copy of the one p1
points to?
note: classB is the base class of the class whose object is passed to
in f1(this)
Ah, I think I know now what you mean. You have a pointer to a
polymorphic base class and want to copy an object of a derived class
that is points to. This is not possible with built-in features of C++.
In your example, you get the base class part sliced off and your copy
will only contain that part. In fact, it only is an instance of the
base class.

You need to write a clone member function that does the job. Something
like:

class Base
{
public:
virtual Base* clone() const = 0;
//...
};

class Derived : public Base
{
public:
virtual Derived* clone() const
{
return new Derived(*this);
}
//...
};

And then your f1 could look like;

f1(Base* p1)
{
Base* p2 = p1->clone();
//...
}
Any suggestion ?? Is copy constructor required? ( I have tried it but
it does not work for pointer objects)


Your above example _does_ use the copy constructor.

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
8
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
8
by: Jason Shohet | last post by:
Perhaps an asp.net question but involves c# classes so I'd rather ask this here. Here goes: Lets say I have an asp.net app called Logon. When a user logs on in Logon, a User class is...
2
by: ndario | last post by:
i am trying to transfer a user to another page with Server.Transfer() but i am having a problem with sending query params like this Server.Transfer("newPage.aspx?param=value"); in the PageLoad...
4
by: Brian | last post by:
Hi, I'm trying to make an online FTP utility in C# ASP.NET using MSINET.ocx (an active X control a.k.a. "Microsoft Internet Transfer Control") I've added the reference into my project and have...
10
by: GreggTB | last post by:
I've got an page (LOGIN.ASPX) that receives the user's login information. During the page load, it checks the credentials against a database and, if validation is successful, creates an instance of...
4
by: Andrew Jackson | last post by:
I am writing a newsgroup client. I have the protocol figured out. But I get slow transfer speeds off any of the network objects read the data from For example one of the commands for a news...
4
by: Donos | last post by:
Hi I have a HANDLE to an Event, like this.. HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL); This is running in one thread in one class. For example we will call that class as "Class A"...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.