Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between const variable and const type variable

Tags:

c++

constants

What is the difference between:

const variable = 10;

and

const int variable = 10;

Does variable, per the standard, get interpreted as an integral type when no type is defined?


1 Answers

const variable = 10 is not valid C++, while const int variable = 10; is.

The only time (that I can think of) that const variable = 10 would be valid is if you had a type named variable and you had a function with an unnamed parameter of that type, taking a default argument:

typedef int variable;
void foo(const variable = 10);
like image 169
James McNellis Avatar answered Apr 12 '26 07:04

James McNellis