#how to init static variables in main () ??
42 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
whats the error and how to resolve it
initialize i in the global scope
because class Solution is defined in the global scope
so whats global scope in cpp
outside main ??
static variabes from a class will be initialized before main starts
global variables also before main starts
main is defined in global space??
yes
got it
functions can be either global, static functions in a class, or member functions in a class
but why we need to define it on global space ?
cus class defined in global space?
.
you can define classes inside of functions
or onside other classes too
but i was doing that only, inside main()
show me
sorry i read that wrong
if i define class inside function then its static variables will also be init inside that function??
yes
Thank you and let us know if you have any more questions!
well, no
if you define a class inside a function, then that class cannot have static data members/static variables
oh okay
if that function is static then also ??
doesn't matter what kind of function you define the class in
if you define a local class inside any function, that class cannot have static data members/static variables
why we put that restriction ??
we cannot have static reserve memory of that class, before running that function ??
never looked into any reason or rationale for that, but at the same time, do you really want that? and assuming you do, what would it mean? do you make the static data member behave more like static data members of non-local classes? or do you make them behave more like static local variables? even just that isn't clear
and considering that those things predate inline variables, how do you even initialize that static data member?
i think also we have reserve extra memory for them, considering we may never execute that function, and their behavior could be the issue as you said.
i may never use it, but playing with it and understanding what made cpp devs to do so is also fun...
btw thanks for that @gray gazelle