Reputation: 648
Im trying to do the copy part of a "deep copy" with my copy constructor:
class myClass
{
public:
myClass ( const char *cPtr, const float fValue )
myClass ( const myClass& myClassT );
private:
const char* &myAddress;
float MyFloater;
};
//myClass.cpp
myClass::myClass( const char *cPtr, const float fValue )
{
// Initialize both private varaible types
const char* &myAddress = cPtr;
float myFloater = fValue;
}
myClass::myClass( const myClass& classType )
{
// copy what we did ...
myAddress = myClass.myAddress;
myFloater = myClass.myFloater;
}
with just that, im getting only the, "must initialize whataver varaible in base/member initalizer list.
They are initalized in the constructor! What would i need to do with the classtype object address?
Upvotes: 0
Views: 658
Reputation: 26140
Try having your code like this:
class myClass
{
public:
myClass ( const char *cPtr, const float fValue );
myClass ( const myClass& myClassT );
private:
const char* &myAddress;
float MyFloater;
};
//myClass.cpp
myClass::myClass( const char *cPtr, const float fValue ) : myAddress(cPtr), MyFloater(fValue)
{
}
myClass::myClass( const myClass& classType ) : myAddress(classType.myAddress), MyFloater(classType.MyFloater)
{
// copy what we did ...
}
Upvotes: 0
Reputation: 89859
Several problems:
1) Why are you repeating the variable declaration in the first constructor?
2) Why are you declaring the myAddress as a reference rather than just as a pointer? A reference always has to be initialized in the constructor initialization list. This is likely the cause of your error message. Initializing in the constructor body is not acceptable to C++.
In the second constructor, you probably want something like:
myClass::myClass( const myClass& classType ):myAddresss(classType.myAddress), myFloater(classType.myFloater)
{
}
The reason for this has to do with how objects are constructed. At the point where the body of the constructor executes, the object is already considered to have been "built" so references should already be valid, so your assignment of a reference is rejected by C++.
And by the way, this is not really a deep copy...
Upvotes: 3
Reputation: 36092
in addition to what others said, you are not really doing a 'deep copy' what you are doing is setting the pointer to point the same, that is not a deep copy. You need to duplicate whatever the pointer is pointing to as well (to be on the safe side).
Upvotes: 3
Reputation: 229934
You are not initializing the members in the constructor, you are declaring two new local variables with the same names as the ones in the class. And initializing them with an initializer list would look like this:
myClass::myClass( const char *cPtr, const float fValue )
: myAdress(cPtr), myFloater(fValue)
{
}
Upvotes: 2
Reputation: 34072
const char* &myAddress;
is a reference, so it must be initialized in an initializer list during the constructor, i.e.
myClass::myClass(const char *& cPtr, const float fValue): myAddress(cPtr)
Note that this is a bad way to handle this situation (of course, depending on what you're trying to do), since if the pointer passed goes out of scope, then you're screwed.
A better way to do this (again, depending on what you're trying to do) is just to copy the pointer over, and during your "deep" copy, reallocate and copy its contents. Unless you actually need a reference to the pointer, of course.
Upvotes: 7