473,594 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy Class from Base

I want to create a new inherited class givin its base. For example:

class Base {
public:
void operator =(const Base &base) {
// copy base items
}
};

class Inherits: public Base {
public:
void operator =(const Inherits &inherits) {
// copy items
}
}

class InheritsAgain: public Base {
public:
void operator =(const InheritsAgain &ia) {
// copy items
}
}

int main(void) {
Inherits inherits;
InheritsAgain ia;

Add(inherits);
Add(ia);
}

Base &CreateNewItem( const &Base base) {
Base *lpbase;

lpbase = new Base;
*lpbase = const_cast<Base &>(base); // Copy new item

return lpbase;
}

I need to fire the Inherits copy function but it keeps wanting to fire
the Base copy function. Is there a better way to do this? I would
prefer the CreateNewItem function to handle the 'new' and 'delete'
operations of these new items. I need a generic function to do this
for all types of classes made from Base.

Nov 25 '05 #1
1 1474
mi***@miben.net wrote:
I want to create a new inherited class givin its base. For example:

class Base {
public:
void operator =(const Base &base) {
// copy base items
}
};

class Inherits: public Base {
public:
void operator =(const Inherits &inherits) {
// copy items
}
} ;

class InheritsAgain: public Base {
public:
void operator =(const InheritsAgain &ia) {
// copy items
}
} ;

int main(void) {
Inherits inherits;
InheritsAgain ia;

Add(inherits);
Add(ia);
What do those do? You haven't declared any 'Add' function.
}

Base &CreateNewItem( const &Base base) {
Base *lpbase;

lpbase = new Base;
Consider definiing and initialising in one statement instead of two:

Base *lpbase = new Base;
*lpbase = const_cast<Base &>(base); // Copy new item
What's the point of this? Couldn't you copy-construct? Like this:

Base *lpbase = new Base(base);

return lpbase;
Oh, and the entire body of your function could be a single return:

return new Base(base);

and no need for any casts or any local variables.
}

I need to fire the Inherits copy function but it keeps wanting to fire
the Base copy function. Is there a better way to do this?
To do exactly what? Have you tried declaring the assignment operator
'virtual'? It probably won't help you, though. What you need is the
'clone' method. A virtual function that creates a perfect copy of the
object for which it's called.
I would
prefer the CreateNewItem function to handle the 'new' and 'delete'
operations of these new items. I need a generic function to do this
for all types of classes made from Base.


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

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

class InheritsAgain: public Base {
public:
virtual Base* clone() const {
return new InheritsAgain(* this);
}
};
V
Nov 25 '05 #2

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

Similar topics

42
5742
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
11
3419
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
10
4002
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects... private: Uncopyable(const Uncopyable&); // ...but prevent copying
6
3926
by: Peng Yu | last post by:
Hi, I'm wondering if the following assignment is lazy copy or not? Thanks, Peng std::vector<intv. v.push_back(1); v.push_back(2);
0
7947
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
7880
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
8255
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...
1
8010
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
8242
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
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
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.