Sign in to follow this  
RidiculousX

= operator usage in dereferenced pointers to classes

Recommended Posts

RidiculousX    140
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
ToohrVyk    1596
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
RidiculousX    140
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this