After some find and replace refactoring I ended up with this gem:
const class A
{
};
What does "const class" mean? It seems to compile ok.
After some find and replace refactoring I ended up with this gem:
const class A
{
};
What does "const class" mean? It seems to compile ok.
If you had this:
const class A
{
} a;
Then it would clearly mean that 'a' is const. Otherwise, I think that it is likely invalid c++.
What does "const class" mean? It seems to compile ok.
Not for me it doesn't. I think your compiler's just being polite and ignoring it.
Edit: Yep, VC++ silently ignores the const, GCC complains.
The const
is meaningless in that example, and your compiler should give you an error, but if you use it to declare variables of that class between the closing }
and the ;
, then that defines those instances as const
, e.g.:
const class A
{
public:
int x, y;
} anInstance = {3, 4};
// The above is equivalent to:
const A anInstance = {3, 4};
It's meaningless unless you declare an instance of the class afterward, such as this example:
const // It is a const object...
class nullptr_t
{
public:
template<class T>
operator T*() const // convertible to any type of null non-member pointer...
{ return 0; }
template<class C, class T>
operator T C::*() const // or any type of null member pointer...
{ return 0; }
private:
void operator&() const; // Can't take address of nullptr
} nullptr = {};
An interim nullptr
implementation if you're waiting for C++0x.
It would be nice to add it to the language. It would mean that every method and variable is implicitly const. Effectively, every instance of that class is automatically const. Here's one possible use:
const class DataTransferObject {
public:
~DataTransferObject() {}
int getX() { return x_; } // const method implied
int getY() { return y_; } // const method implied
private:
friend class Factory;
DataTransferObject(int x, int y): x_(x), y_(y) {}
int x_; // const implied after constructor exits
int y_; // const implied after constructor exits
};
c++ help says it extends const to classes... so what u r talking is rubbish, compiler isn't ignoring the const modifier it does have some meaning, but what?