#DOUBT
70 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below 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 use !howto ask.
you’re defining the variable here
static int x; is just a declaration
it simply says “x exists somewhere”
if you do Sally::x = 10; to modify the value you’ll get the compilation error you expect
but can I define variables outside class? Especially private variables, that seems to violate the privacy!
“no one can create” wrong 😛
x is not accessible from outside the class but you still need to define the variable
I see, I guess private variables means no one can access or modify, but they can create if given a declaration already using ::
yes
only for static members
the variable must be defined in exactly one cpp file
or
you can declare it as inline and initialize it directly in the class
I cant define in some other file?
when tou simply declare it as a static variable you MUST define it in exactly ONE cpp file
it’s the same thing like doing extern int x;
Oh, I thought static variable just means this variable belongs to this class
also they cant really define their own value to it because , it needs to be intialized only once , and you will already do it somewhere (hopefully) in your .cpp file or somewhere
It seems there are some limitations, nothing is straightforward!
what does “belongs to this class means”?
Means it will not be object's
only class values i suppose
do u understand the difference between a regular class member and a static member ? assume all of them are public for now
a static variable is a global variable, just only accessible from the scope of a class
nothing more
the same rules for globals apply
you either declare them in the header and define them in exactly one cpp file, or you declare them as inline from the start
class Sally {
private:
static inline int x = 5;
};```
I see
My understanding was intuitive I guess
it was correct in the sense that the variable is not a member of a class instance, you just missed the part that, other than that, it’s a regular global variable with different access rights
you can avoid future confusion by just using inline
different access rights are defined by public, private or protected right?
ye
okay
The same is true for member functions
How else would you move definitions from a header to a .cpp file if this wasn't allowed?
Yeah thats true, i did not correlate
Btw this in same file would only work for static members though which in that case can be any other functions, not just static
i hope i m saying right
I'm not sure I understand the question, but non-static data members never need definitions outside of the class
if you do this in the same file, and specifically a header, you’ll get linker errors
if I understand the “same file” part correctly
but in cpp file of class, we give definitions to non static functions
Are you asking why non-static functions need definitions in .cpp, but non-static data members don't?
yes, except I meant in cpp file where my main function reside, but you can mean that way too
Each instance of a class stores its own fields. But it doesn't store member functions, even non-static ones
Non-static member functions are basically same as free functions, but with a hidden this parameter
suppose I have :
class cat{
void sound(){
std::cout<<"Meow";
}
};
int main(){
cat c;
c.sound();
}
the instance c of cat class stores the function sound otherwise how could it have worked?
i am sorry if i dont understand what you mean there
this
Your code is no different from
class cat {};
void sound(cat *c)
{
std::cout << "Meow";
}
int main()
{
cat c;
sound(&c);
}
Non-static member functions are just syntax sugar for this (meaning they have "nicer" spelling, but work like this under the hood)
You can check this with sizeof(cat). It grows when you add non-static member variables, but not member functions
thats new for me
Have you learned pointers yet?
A bit, i learned till pass by pointer, pass by reference, pass by value and some arithmetic where we add pointer by 1 or 2, and it changes addresses to next element in array
I did learn this pointer , but it has been years so I have no revised that part of my notes
this is basically the name of the hidden pointer parameter that points to object that the function is called on
So c in my function is same as this in yours
how simple things would have been if cpp was not hiding so much things or layers beneath in hood
The syntax would be ugly though 😛
agreed but its better to teach from abcd and then go advanced, by simplifying the syntax imo
Yeah, I agree
Learning C first is somewhat unpopular now, but it helps with stuff like this
java too
This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.