473,655 Members | 3,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2176
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
2369
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. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
8
1617
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
1427
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 populated with his / her name, department etc. After the user has logged in I present him with a page that lets him go to other websites -- app1 & app2. The problem is when he is redirected over to app1 or app2, I want that User class to come with...
2
2016
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 method of the newPage.aspx.cs, Request.QueryString is allways null. is there any way to transfer to another page, but to recive a param on the another page. i even try with response.redirect, but it doesn't help. also, puting value in the...
4
3787
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 been able to compile it. However, when I go to a webpage, and try to FTP something, I get this error: System.Web.HttpUnhandledException was thrown.(Class is not licensed for use)
10
12192
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 an object that stores the user's basic profile data (username, user type, associated sales region, etc.). I've been taking this user info and placing it in the Session object like so... Session = user;
4
7580
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 client to use is "XOVER articlenumber-" This return string after string of all the news articles from article number on.... Another newsclient, i wont name names, pulls data down just fine. Using a
4
4303
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" Now i want to use this HANDLE in another thread in another class to
20
4029
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 tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.