A
Allen
1. why can not static member function be declared as const?
class Demo
{
public:
static int method() const; // error!!
};
2. is this kind of const_cast usage secure?
class Demo
{
public:
static const int VALUE = 12;
void Read(int* pValue, int offset)
{
if (offset < 0 || offset >= 2)
return;
value[offset] = *pValue;
}
private:
int value[2];
};
int main()
{
int val1 = 0, val4 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0); // absolutely ok
demo.Read(const_cast<int*>(&val2), 1); // is const_cast secure here?
}
class Demo
{
public:
static int method() const; // error!!
};
2. is this kind of const_cast usage secure?
class Demo
{
public:
static const int VALUE = 12;
void Read(int* pValue, int offset)
{
if (offset < 0 || offset >= 2)
return;
value[offset] = *pValue;
}
private:
int value[2];
};
int main()
{
int val1 = 0, val4 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0); // absolutely ok
demo.Read(const_cast<int*>(&val2), 1); // is const_cast secure here?
}