• Advertisement
Sign in to follow this  

= operator usage in dereferenced pointers to classes

This topic is 3943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement