#DOUBT

70 messages · Page 1 of 1 (latest)

coral glen
#

Question :

private:
    static int x;
};

int Sally::x = 5;```

why is this valid, if variable is private, privacy means no one can create, access or modify!
cerulean carbonBOT
#

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.

dawn reef
#

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

coral glen
#

but can I define variables outside class? Especially private variables, that seems to violate the privacy!

dawn reef
dawn reef
coral glen
#

I see, I guess private variables means no one can access or modify, but they can create if given a declaration already using ::

dawn reef
#

yes

dawn reef
#

the variable must be defined in exactly one cpp file

#

or

#

you can declare it as inline and initialize it directly in the class

coral glen
dawn reef
#

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;

coral glen
#

Oh, I thought static variable just means this variable belongs to this class

potent warren
coral glen
#

It seems there are some limitations, nothing is straightforward!

dawn reef
coral glen
#

only class values i suppose

potent warren
dawn reef
#

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;
};```
coral glen
dawn reef
#

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

dawn reef
coral glen
dawn reef
#

ye

coral glen
#

okay

autumn atlas
#

How else would you move definitions from a header to a .cpp file if this wasn't allowed?

coral glen
#

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

autumn atlas
#

I'm not sure I understand the question, but non-static data members never need definitions outside of the class

dawn reef
#

if I understand the “same file” part correctly

coral glen
autumn atlas
#

Are you asking why non-static functions need definitions in .cpp, but non-static data members don't?

coral glen
#

yeah

#

non static functions are non static data members only though

coral glen
autumn atlas
#

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

coral glen
autumn atlas
#

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

autumn atlas
#

Have you learned pointers yet?

coral glen
#

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

autumn atlas
#

So c in my function is same as this in yours

coral glen
#

how simple things would have been if cpp was not hiding so much things or layers beneath in hood

autumn atlas
#

The syntax would be ugly though 😛

coral glen
autumn atlas
#

Yeah, I agree

#

Learning C first is somewhat unpopular now, but it helps with stuff like this

coral glen
#

java too

cerulean carbonBOT
#

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.