Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "const class" mean?

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.

like image 504
1800 INFORMATION Avatar asked Oct 16 '08 00:10

1800 INFORMATION


People also ask

What is a const class?

Const member functions in C++ C++ProgrammingServer Side Programming. The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided.

What does const mean in coding?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

What const means?

Const (constant) in programming is a keyword that defines a variable or pointer as unchangeable. A const may be applied in an object declaration to indicate that the object, unlike a standard variable, does not change. Such fixed values for objects are often termed literals.

Can you make a class A const?

Like member functions and member function arguments, the objects of a class can also be declared as const. an object declared as const cannot be modified and hence, can invoke only const member functions as these functions ensure not to modify the object.


1 Answers

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}; 
like image 67
Adam Rosenfield Avatar answered Oct 01 '22 00:10

Adam Rosenfield