This code compiled.
struct A
{
const int *getX() const
{
return &x;
}
int *getX()
{
const A *thisConst = this;
return const_cast<int *>(thisConst->getX());
}
void f()
{
int *p = getX();
}
int x;
};
But this code didn't.
struct I
{
virtual const int *getX() const = 0;
int *getX()
{
const I *thisConst = this;
return const_cast<int *>(thisConst->getX());
}
};
struct A : I
{
virtual const int *getX() const
{
return &x;
}
void f()
{
int *p = getX();
}
int x;
};
'const_cast' : cannot convert from 'const int *' to 'int *'
I know that if I will give different names it will be compiled. But are there ways without functions renaming?
I didn't get this error while trying to compile your program, instead I got
To compile successfully I corrected following line
to
const int *
andint*
are two different types, you can't assign it directly without cast or modify the type of the variablep
.