Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

inheritance and copy constructor conflict

I have a class TypeCORD2R inheriting TypeCORD2C.

before putting in a copy constructor, the static_casts in the == and < operators work without any problems.

class TypeCORD2R: public TypeCORD2C
{
public:
TypeCORD2R(string & data);
};
bool operator<(const TypeCORD2R & itemA, const TypeCORD2R & itemB);
bool operator==(const TypeCORD2R & itemA, const TypeCORD2R & itemB);

bool operator==(const TypeCORD2R & itemA, long itemB)
{
if(itemA.CID==itemB)
return true;
else
return false;
};
bool operator==(const TypeCORD2R & itemA, const TypeCORD2R & itemB){
return ((static_cast<TypeCORD2C>(itemA))==(static_cast<TypeCORD2C>(itemB)));
};
bool operator<( const TypeCORD2R & itemA, const TypeCORD2R & itemB){
return ((static_cast<TypeCORD2C>(itemA))<(static_cast<TypeCORD2C>(itemB)));
};
TypeCORD2R::TypeCORD2R(string & data):TypeCORD2C(data)
{
//no code needed, all work done by parent
}

However, once I introduce a copy constructor into TypeCORD2C as below, the static casts in the operators above all of a sudden become problems.

the error is:
error C2440: 'static_cast' : cannot convert from 'const TypeCORD2R' to 'TypeCORD2C'
No constructor could take the source type, or constructor overload resolution was ambiguous

any ideas?

class TypeCORD2C: public TypeBase
{
public:
long CID;
long RID;
float A[3];
float B[3];
float C[3];
TypeCORD2C(string & data);
TypeCORD2C(TypeCORD2C & cord2cIn);
friend class TypeCORD2R;
list<string> toLines();
};
bool operator==(const TypeCORD2C & itemA, const TypeCORD2C & itemB);
bool operator<(const TypeCORD2C & itemA, const TypeCORD2C & itemB);

bool operator==(const TypeCORD2C & itemA, long itemB)
{
if(itemA.CID==itemB)
return true;
else
return false;
};
bool operator==(const TypeCORD2C & itemA, const TypeCORD2C & itemB){
if(itemA.CID==itemB.CID)
return true;
else
return false;
};
bool operator<( const TypeCORD2C & itemA, const TypeCORD2C & itemB){
if(itemA.CID<itemB.CID)
return true;
else
return false;
};

TypeCORD2C::TypeCORD2C(TypeCORD2C & cord2cIn)
{
CID = cord2cIn.CID;
RID = cord2cIn.RID;
for (int i = 0; i<3 ; i++)
{
A[i] = cord2cIn.A[i];
B[i] = cord2cIn.B[i];
C[i] = cord2cIn.C[i];
}
}
TypeCORD2C::TypeCORD2C(string & data)
{
//do stuff
}
[2820 byte] By [rssmps] at [2007-11-11 8:08:59]
«« Lisp
»» i need help
# 1 Re: inheritance and copy constructor conflict
Never mind..... I figured it out.

I was missing a const in the copy constructor parameter.

PS, you can't delete your own post?
rssmps at 2007-11-11 21:01:37 >
# 2 Re: inheritance and copy constructor conflict
You can delete the text in the message and ask me to delete your post. Normally, users are not allowed to delete their own posts because this could lead to various ethic problems such as deleting wholesale deletion of posts from a lively thread, which means that only the replies to the posts will remain dangling.
Danny at 2007-11-11 21:02:37 >