Reputation: 2695
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?
Upvotes: 1
Views: 49
Reputation: 15844
'const_cast' : cannot convert from 'const A *' to 'int *'
I didn't get this error while trying to compile your program, instead I got
error: invalid conversion from 'const int*' to 'int*' [-fpermissive]
To compile successfully I corrected following line
int *p = getX();
to
const int *p = getX();
const int *
and int*
are two different types, you can't assign it directly without cast or modify the type of the variable p
.
Upvotes: 1