Data Member and Static Keyword in C++

Data Member and Static Keyword in C++

data members

Hey, what's up guys. My name is Tariq and welcome back to my C++ series Today just coming at you with a quick article and we're going to talk about what static means in C++ so the static Keyword in C++ actually has two meanings depending on what the Context is one of those applies to when you use the static Keyword Outside of a class or a struct and the other is when you use static Inside a class or struct. Basically just to cut to the chase, static outside of class means that the linkage of that symbol that you declare to be static is going to be internal meaning. It's only going to be visible to that translation unit that you've defined it in. 


Whereas a static variable inside a class or struct means that variable is actually going to share memory with all of the instances of the class meaning that basically across all instances that you create of that class or struct, there's only going to be one instance of that Static variable and a similar thing applies to static methods in a class there is no Instance of that class being passed into that method. We're going to talk a lot more in depth about what? Static actually means in a class or struct scope so definitely check out that video It'll be a card on the screen and a link in the description below but today We're just going to focus on what static means outside Of that class or struct scope. So let's dive into some code and take a look.


So what I'm going to do here in this completely empty C++ file is just define a variable to be static I'm going to use the s_ convention here to basically indicate that this variable is static And I want to just set it equal to five okay cool So it looks like any other variable, but it's got the static keyword in front of it What does that actually mean what it means? Is that this variable is only going to be linked? internally inside this Translation unit. If you don't know how C++ Compilation or linking works definitely check out my videos on how the c++ compiler works And how the C++ linker works because you really need to understand what's going on there to understand this. 


A static variable or function means that when it actually comes time to link those actual functions or variables to Actually define symbols, the linker is not going to look outside of the scope of this translation unit for that symbol definition Yeah again, this is one of those things that's best explained by examples Let's take a look. We've clearly created a variable here and set it equal to five what's going to another C++ file into another translation unit yeah, I've just got a blank C++ file with a main function We're going to create a global variable here. I'm going to give it exact same name as the static one I'll set this equal to like ten or something like that if I try and print this variable you can see that we can compile our code and we're not going to get any problems and if I run my code of course we'll get ten printing out to the console however if I go back to my static dot CPP file and I remove the static Keyword And I try and compile my code you'll see that when it gets to the linking stage we actually get a linking error because this s Variable has already been defined in each different Translation unit so we can't have two global variables with the same name. 


One of the things I could do to fix this is change this variable to actually refer to this one and the way that I can do that is by getting rid of the assignment here and marking this variable as Extern which means that it's going to look for that s variable in an external Translation unit which is called external linkage or external linking.


 If I run my code now, you can see the value We get is five because of course it's referencing this s variable. However if I come over here, and I mark this static It's kind of like declaring a variable private in a class no other translation unit is going to see this s_Variable the Linker will not see this in a global scope which means that if I go back here And I try and compile my code you can see that we actually get an unresolved external Sample s_Variable because it cannot find an integer with the name s_Variable anywhere Because we've effectively marked our variable as private if we go back here I'm also going to declare a function to show you guys that it does the same thing if I declare a function here called function and another function over here Which has the same signature is also going to return nothing is going to be void if I try and compile this, now I'm going to get a duplicate symbol error at the linking stage because there are two functions if I go over here into static and I mark this one as Static then of course when the linker stuff to link all this it's not going to see the static one and thus I'm not going To get any errors let's try and compile this you can see I don't get the error anymore for the function I still get the s_Variable one let's just get rid of that and the print as well. If I compile this we get no errors. So that's really all the static means when you use it outside of a class or a struct in C++. 


It just means that when you declare that static function Or that static variable is only going to be visible to that C++ file that you've declared it in if you want to declare a static variable in a header file and include that header file in two different C++ files and effectively what you've done is what I've kind of done with my example where I've had that same s_Variable declared as a static variable in both Translation units because of course when you include that header file It's going to copy all the contents and paste it into that C++ file So what you've done is You just created a static variable in two different translation units as to why you would use this think about why you would use private in a class kind of environment you basically want to use static as Much as you can if you don't need the variable to be global because as soon as you start declaring things in a global scope without that static Keyword You can see that the link is going to pick it up across translation units which means that you've created a variable that is Completely global.


If I create a variable with the name variable, and it's in a global setting Suddenly That that name variable is completely global and can be used anywhere And that can lead to some pretty bad bugs and that comes down to the whole thing that global variables are bad Which I don't particularly agree with but the point is try and mark your functions and your variable static unless you actually need them to be linked across Translation units.

Comments

Popular posts from this blog

Friend Function in C++

Use Of CPP