| | |
Copy constructor in derived class
![]() |
•
•
Join Date: Sep 2004
Posts: 4
Reputation:
Solved Threads: 0
I'm having a bit of trouble finding the info I need about copy constructors. Actually, I'm having a bit of trouble figuring out the right way to use them in the first place.
My question: How do I correctly call copy constructors for a derived class so that it correctly copies data from the base class as well? Here's what I've come up with.
class Base
{
Base();
Base(const Base &theObject);
int baseData;
int GetData1() const {return baseData;}
};
Base::Base(const Base &theObject)
{
baseData = theObject.GetData1() ;
}
class Derived : public Base
{
Derived();
Derived(const Derived &theObject);
int derivedData;
int GetData2() const {return baseData;}
}
Derived:: Derived( const Derived &theObject)
{
Base( &theObject );
derivedData = theObject.GetData2();
}
Is this what a copy constructor is supposed to do?
My question: How do I correctly call copy constructors for a derived class so that it correctly copies data from the base class as well? Here's what I've come up with.
class Base
{
Base();
Base(const Base &theObject);
int baseData;
int GetData1() const {return baseData;}
};
Base::Base(const Base &theObject)
{
baseData = theObject.GetData1() ;
}
class Derived : public Base
{
Derived();
Derived(const Derived &theObject);
int derivedData;
int GetData2() const {return baseData;}
}
Derived:: Derived( const Derived &theObject)
{
Base( &theObject );
derivedData = theObject.GetData2();
}
Is this what a copy constructor is supposed to do?
•
•
Join Date: Sep 2009
Posts: 1
Reputation:
Solved Threads: 0
You can also do the same for the = operator.
In case anyone was curious.
c Syntax (Toggle Plain Text)
Base operator=(const Base& rhs) { //copy Base } Derived operator=(const Derived& rhs) { Base::operator=(rhs); //copy derived }
In case anyone was curious.
![]() |
Similar Threads
- Does it need a copy constructor and an operator= for a class which have array member (C++)
- (C++) Writing a Copy Constructor?!? (C++)
- need the usage of Copy Constructor (C++)
Other Threads in the C Forum
- Previous Thread: Solve the problem
- Next Thread: pointer copy using for loop
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h





