473,386 Members | 1,841 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,386 software developers and data experts.

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 1465
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
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...
11
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...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
6
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
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...

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.