Connecting Tech Pros Worldwide Forums | Help | Site Map

const..ant problems

Gernot Frisch
Guest
 
Posts: n/a
#1: Jul 23 '05
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


Gernot Frisch
Guest
 
Posts: n/a
#2: Jul 23 '05

re: const..ant problems



"Gernot Frisch" <Me@Privacy.net> schrieb im Newsbeitrag
news:34hoi2F4cg86jU1@individual.net...[color=blue]
> 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?[/color]

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.



Ioannis Vranos
Guest
 
Posts: n/a
#3: Jul 23 '05

re: const..ant problems


Gernot Frisch wrote:[color=blue]
> 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?[/color]



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




--
Ioannis Vranos

http://www23.brinkster.com/noicys
Closed Thread


Similar C / C++ bytes