What does "const class" mean?

28,257

Solution 1

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};

Solution 2

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++.

Solution 3

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.

Solution 4

Try compiling it with GCC, it will give you below error:
error: qualifiers can only be specified for objects and functions.

As you can see from the error that only objects(variables, pointers, class objects etc.) and functions can be constant. So try making the object as constant, then it should compile fine.
const class A {};
const A a ;

Share:
28,257
1800 INFORMATION
Author by

1800 INFORMATION

Multitudinis imperitæ non formido judicia; meis tamen, rogo, parcant opusculis——in quibus fuit propositi semper, a jocis ad seria, a seriis vicissim ad jocos transire.

Updated on July 09, 2022

Comments

  • 1800 INFORMATION
    1800 INFORMATION almost 2 years

    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.

  • Robert Gould
    Robert Gould over 15 years
    Good old forgotten features of C++! I do remember doing stuff like that in C long ago thou.
  • MSalters
    MSalters over 15 years
    I can't find a justification to make this an error. Compilers may emit warnings at will, but only the standard determines what counts as an error.
  • Tom Duckering
    Tom Duckering over 15 years
    Because you're declaring something as const that isn't. This is something that is never what you want to do, and so should be an error. The standard should determine what is correct; defining every wrong thing would be too much work.
  • Shariful
    Shariful over 15 years
    I think that the term used in the standard is "illformed". The standard doesn't say what isn't allowed, only what is. This isn't valid C++ syntax, therefore isn't allowed.
  • Richard Corden
    Richard Corden over 15 years
    It's an error because the storage class specifiers are only applicable if you have a declarator and in this example there is no declarator.
  • Johannes Schaub - litb
    Johannes Schaub - litb over 14 years
    7.1.5.1/1: "There are two cv-qualifiers, const and volatile. If a cv-qualifier appears in a decl-specifier-seq, the init-declarator-list of the declaration shall not be empty."