C++ question constants in classes?

Ok so i endeavor to declare some sort of const value in the class although my compiler claims “ISO c++ forbids initialization involving member (variable name)” ive looked over the web and apparently you have to put your static keyword looking at const, my concern is PRECISELY WHY (im a working male who likes to be aware of why points happen)

Const new member and const static person are two unique concepts.I utilize both quite often.

Const member is often a member that’s initialized whenever an object of category is created, and is usually never changed insurance policy coverage object is out there.There is really a separate, several const member in each object, possibly which has a different cost.The caveat:default work operator will no more work, if you want your objects being assignable, you’ll need to write your assignment operator that may not modify the const member

school Foo

const int n;
criminal court:
Foo(int i):n(i)
;
int main()

Foo f1(1); // ctor.f1.n is developed here, f1.n == 1
Foo f2 = A COUPLE OF; // duplicate ctor.f2.n is developed here, f2.n == 2
// f2 = f1; // assignment won’t compile, f2.n is const

const static member of an class is an object along with static period, meaning singular exists, which is created if your program starts running, which is deallocated if your program halts running.It is just like a global consistent, except that usage of it is definitely controlled by class accessibility rules.Which has a few conditions, static members needs to be initialized on namespace scope, outside the actual class:

#include
#include
class Foo // this will likely be inside a header

static const a sexually transmitted disease::string in;
criminal court:
a sexually transmitted disease::string getn() const give back n;
;
const a sexually transmitted disease::string Foo::n = “foo”; // not in header, and here , Foo::n is created
int main()

Foo f1; // Foo::n already is available, it isn’t created here
a sexually transmitted disease::cout << f1.getn() << '\n';

I think it is because you are trying to use this like….classClass.constConst…for example..if you attempt to use that like that you must initialize your constant inside the class you intend to call.else you should create a different instance with the class…expect this allows.Lemme recognize if it does.

Leave a Reply