Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
hi why is it possible to declare a static constant member in a class but not a constant member?
Posted

It is definitely possible to declare and use a non-static constant member in C++ classes.
See this[^] form Bjarne Stroustrup's C++ Style and Technique FAQ. :)
 
Share this answer
 
Do you mean initialize the variable in-class ?

A static const is a variable that is not going to change its value for all instances of the class.

A const variable on the other hand can have a value that doesn't change for one instance of the class, but can be different for different instances.

A static const example -
class A
{
public:
    static const int var = 100;
};


A const example -
class A
{
public:
    const int var;

    A(int a) : var(a) {}
};

int main()
{
    A a(10);
    A b(20);
}
 
Share this answer
 
I want to see it
just as an optimization of the memory usage... :-D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900