const..ant problems

G

Gernot Frisch

Hi,

assume:

MyClass mc;
const MyClass& Rmc = mc;

mc->GetPtr()->AlterData();
Rmc->GetPtr()->ReadData(); // Here's the error

now: I have defines MyClass as:

class MyClass
{
public:
MyClass() {};
MyClass* GetPtr() {return this;}
const MyClass* GetPtr() const {return this;}

void AlterData() {}
void ReadData() const {}
};

However, this does not work.

Error C2440: 'const class MyClass*' cannot be converted to 'class
MyClass *'

What did I do wrong? Is the compiler always using the first one? Do I
have to use 2 function names?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
G

Gernot Frisch

Gernot Frisch said:
Hi,

assume:

MyClass mc;
const MyClass& Rmc = mc;

mc->GetPtr()->AlterData();
Rmc->GetPtr()->ReadData(); // Here's the error

now: I have defines MyClass as:

class MyClass
{
public:
MyClass() {};
MyClass* GetPtr() {return this;}
const MyClass* GetPtr() const {return this;}

void AlterData() {}
void ReadData() const {}
};

However, this does not work.

Error C2440: 'const class MyClass*' cannot be converted to 'class
MyClass *'

What did I do wrong? Is the compiler always using the first one? Do
I have to use 2 function names?

Doh! The error was there:
MyClass* pC = Rmc->GetPtr();


I've not done much with const, so I got confused by the error, sorry
for wasting your time.
 
I

Ioannis Vranos

Gernot said:
Hi,

assume:

MyClass mc;
const MyClass& Rmc = mc;

mc->GetPtr()->AlterData();
Rmc->GetPtr()->ReadData(); // Here's the error

now: I have defines MyClass as:

class MyClass
{
public:
MyClass() {};
MyClass* GetPtr() {return this;}
const MyClass* GetPtr() const {return this;}

void AlterData() {}
void ReadData() const {}
};

However, this does not work.

Error C2440: 'const class MyClass*' cannot be converted to 'class
MyClass *'

What did I do wrong? Is the compiler always using the first one? Do I
have to use 2 function names?



mc.GetPtr()->AlterData();
Rmc.GetPtr()->ReadData();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top