Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such a thing as a const constructor?

Today I accidentally put const at the beginning of my constructor (copy paste mistake) and it compiled. I've only tried this in Visual Studio 2008 which is pre C++11. Does this syntax have any meaning? Is this Microsoft's early attempts at constexpr?

class foo
{
public:
    const foo(int i){}
};

foo f(1);
like image 381
cppguy Avatar asked Apr 03 '15 17:04

cppguy


People also ask

Can a constructor be const?

Constructors may be declared as inline , explicit , friend , or constexpr . A constructor can initialize an object that has been declared as const , volatile or const volatile . The object becomes const after the constructor completes.

How do you write a const in C++?

To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition.

What is const Flutter?

The const keyword is the variable value which never changes at compile time. In short, the compiler predicts what value is to be stored in the variable itself. Flutter spontaneously gathers variable type information when you confirm and iprocess the same as a const variable.

How do you define a constructor in darts?

Dart defines a constructor with the same name as that of the class. A constructor is a function and hence can be parameterized. However, unlike a function, constructors cannot have a return type. If you don't declare a constructor, a default no-argument constructor is provided for you.


1 Answers

Your code is not standard compliant, there is no such thing. However, starting with C++11, you can have constexpr constructors, so your object is constructed at compile time and can further be used in constexpr expressions.

Although I am not using it, MSVS is not the best compiler in terms of standard-compliance, at least that's what I realized from various questions on this site.

like image 134
vsoftco Avatar answered Oct 31 '22 04:10

vsoftco