# = operator usage in dereferenced pointers to classes

## Recommended Posts

Language is C++. I'm having trouble getting code like this compile (heavily simplified):
// class definition
class CClass
{
public:
CClass& operator =( CClass& Other ) { ... }
};

//
void* pValue = [some data];
void* pNewValue = [some data];
{
// this works fine, as with any of the other built in types
int* pTemp = (int*)pValue;
*pTemp = *((int*)pNewValue);
}
{
// but when i try to use one of my classes, it doesn't compile
// the errors generated are:
// .\Source.cpp(78) : error C2296: '*' : illegal, left operand has type 'CClass *'
// .\Source.cpp(78) : error C2297: '*' : illegal, right operand has type 'CClass *'
CClass* pTemp = (CClass*)pValue;
*pTemp = *((CClass*)pNewValue);  // edit: was (int*), now (CClass*)
}


If anyone could give me some pointers as to what's wrong here, that would be great. I'm stumped on this one. I'm thinking maybe an overloaded = operator but I honestly have no idea. Thanks in advance. EDIT: fixed incorrect code casting to int instead of CClass [Edited by - RidiculousX on May 5, 2007 5:52:06 PM]

##### Share on other sites
1° Use reinterpret_cast instead of C-style casts.

2° I cannot seem to reproduce your error with the code:

class CClass {  CClass operator=(const CClass &) { return *this;}};int main(){  void* pValue;  CClass* pTemp = reinterpret_cast<CClass*>(pValue);  * pTemp = * reinterpret_cast<CClass*>(pValue);}

##### Share on other sites
Argh, the code I put up was not correct. See the edit note for the change. It seems like you were able to look around that though.

I tried the reinterpret_cast<> thing and it seems to have worked. Thanks!

And oh crap, it turns out I forgot a semicolon on after one of the lines and that's what was generating the error.

Anyway, I could have solved this on my own without reinterpret_cast<>. I'm so mad.

Thanks again though.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628370
• Total Posts
2982300

• 10
• 9
• 13
• 24
• 11