If I have a class like
class MyClass {
public:
int myMember1;
int myMember2;
int myMember3;
};
Every time I instantiate an object of MyClass space for three consecutive int is allocated, what about when I have something like
class MyClass {
public:
static int myMember1;
int myMember2;
int myMember3;
};
How the memory is allocated this time?
I'm asking because I'm not entirely sure how the memory would be allocated when I declare multiple instance of the same class, is there a pointer to the static member maybe?
As others already stated, you have to explicitly allocate space for the static member variable outside the class definition.
In response to your other question, static member variables are not associated with class objects. That is to say, they will continue to exist even after your MyClass object has ceased to exist (until the termination of your program), and be shared across all instances of your class.
Say you created multiple instances of the MyClass class like so:
class MyClass {
public:
static int myMember1;
int myMember2;
int myMember3;
};
int MyClass::myMember1 = 1;
int main()
{
MyClass mc1;
MyClass mc2;
mc2.myMember1 = 2;
std::cout << mc1.myMember1 << '\n';
std::cout << mc2.myMember1 << '\n';
return 0;
}
The output will be:
2
2
How the memory is allocated this time [with the static member]?
Each instance of the object will have 2 integers in, and all instances have access to the static integer (but don't "own" it) - it is not part of the instances, it is in the scope of the class.
N.B. the member is declared in the class, but must be defined outside the class (in the cpp file), e.g.;
int MyClass::myMember1 = 42;
... is there a pointer to the static member maybe?
No. You can get a pointer the static member if you require, but one is not allocated to each instance.
The static member is allocated (and initialised as per the initialisation in the cpp file) when the application launches and can be accessed as other "global" objects are (albeit that the static is not in the global namespace, there is only one instance of it). The accessibility of the member (i.e. public, private vs. protected) follows the normal rules.
To see the effect on the size, you can use sizeof();
class MyClass {
public:
int myMember1;
int myMember2;
int myMember3;
};
class MyClass1 {
public:
static int myMember1;
int myMember2;
int myMember3;
};
int MyClass1::myMember1 = 42;
int main(int argc, char* argv[])
{
using namespace std;
cout << sizeof(MyClass) << " " << sizeof(MyClass1) << endl;
}
The above (depending on alignment and the size of the int), could produce an output of 12 8.
Demo
You have to define the variable outside the class also. That is where the actual allocation will be done.
In essence it is the same as a global variable.
class Test
{
public:
static int too; // Just a declaration
};
int Test::too; // Actual allocation, every instance will use this
You have to explicitly allocate memory for that static member somewhere.
For example, your class in in it's header file:
// myclass.h
class MyClass {
public:
static int myMember1;
int myMember2;
int myMember3;
};
And you have a cpp file for that class, which explicitly allocates space for that static member, it can also initialize it:
// myclass.cpp
int MyClass::myMember1 = 5;
So your static member will be allocated at exactly one place in your program, in the translation unit of your choice. You can place this allocation into any file you want, as long as it is only part of one translation unit.
Static membes are allocated in static memory
You could think in these members as global variables, but declared inside the class scope (with corresponding access, in your case, public).
Related
I am programming c++ in Unreal Engine. I have some functions that use variables whose value I need to last in time and that are only used within that function. The problem is that if I declare them as static local variables, when I have several members of the same class they all share the same value, and I would like each class to have its own instance of a local variable that lasts over time. How can I achieve this?
If you really want that level of encapsulation, you could make a separate class for each of your member variables and put the function(s) that operate on that variable in that class. You can then inherit from any number of those separate classes to build a class that has several variables.
Example:
struct func_x {
func_x(int X) : x{X} {}
int function_using_x() {
return x;
}
private:
int x; // only one member variable
};
struct func_y {
func_y(int Y) : y{Y} {}
int function_using_y() {
return y;
}
private:
int y; // only one member variable
};
struct foo : func_x, func_y { // inherit from both
foo(int x, int y) : func_x(x), func_y(y) {}
};
Usage:
#include <iostream>
int main() {
foo f{1, 2};
std::cout << f.function_using_x() << f.function_using_y() << '\n'; // 12
}
you need 2 variables for that.
one for counter (declare local but nut static) and change in one instance and another static for following the first variable;
but notice every time you need that static you should update it
I'm doing C++ after along time , i had declared a static variable inside the class as private and as far i know the static variables are independent of objects and shared across the objects .If i try to print the static variable outside the class using a class name i get the compilation errors is this because the variable is private ? I did read that the static variables can be accessed just by the Class name and the scope resolution operator .
#include <iostream>
using namespace std;
class Sample{
int val;
static int value;
public:
Sample(int in);
Sample();
void setval(int in){
val = in;
}
void printval ()const{
cout << val<<endl;
}
};
Sample::Sample(int in){
val = in;
}
Sample::Sample(){
val = 0;
}
int Sample::value = 34;
int main()
{
const Sample obj(1);
Sample obj2;
obj2.printval();
obj.printval();
cout <<"static value = " << Sample::value;
return 0;
}
Error
main.cpp:37:5: error: 'int Sample::value' is private
int Sample::value = 34;
^
main.cpp:49:39: error: within this context
cout <<"static value = " << Sample::value;
as far i know the static variables are independent of objects and shared across the objects
That is correct. However, sharing variables across object instances and making variables accessible are independent of each other. There are four combinations of (shared, accessible) pairs. All four are available to you:
public static variable is shared and accessible outside the class
private static variable is shared, but not accessible outside the class
public non-static variable is not shared, but accessible outside the class
private non-static variable is neither shared nor accessible outside the class
Note that the way you deal with the private static value can be modeled after the way you work with non-static val, i.e. by giving your class users some public member-functions to work with the static variable:
class Sample {
...
public:
static int getvalue() { return value; }
};
Now you can print it like this:
cout << "static value = " << Sample::getvalue();
Private class members and methods are accessible only by the class's members and methods. This is true whether the class member is static, or not. This has no influence on the accessibility of the class member.
Note that a public class method has access to private class members, just like any other method, and this does not preclude a public class method from returning a pointer or a reference to the private class members. That's one option for you.
I'm trying to declare a static object of a class A that I wrote in a different class B, like this:
class A // just an example
{
int x;
public:
A(){ x = 4; }
int getX() { return x; }
};
class B
{
static A obj1; // <- Problem happens here
public:
static void start();
};
int main()
{
B::start();
}
void B::start()
{
int x = obj1.getX();
}
What I want to achieve is to get int x in B::start() to equal int x in class A (4).
I tried googling all this for the past hour and all I understood was that C++ doesn't allow static objects' declarations. Is that correct?
If so, here's my question. How can I get the same result? What are my available workarounds? Keeping in mind that the rest of my code depends on the functions in class B to be static.
Error
error LNK2001: unresolved external symbol "private: static class A B::obj1"
Thanks!
You should initialize static var, the code:
class A // just an example
{
int x;
public:
A(){ x = 4; }
int getX() { return x; }
};
class B
{
static A obj1; // <- Problem happens here
public:
static void start();
};
A B::obj1; // init static var
int main()
{
B::start();
}
void B::start()
{
int x = obj1.getX();
}
As thinkerou said, you need to include the declaration of the variable:
A B::obj1;
For normal, non-static member variables you don't need this step because the variables are declared behind the scenes as part of the constructor. These variables are then tied to the instance of the class you just constructed. But static variables are not tied to any instance of a class; they are shared by all instances of a class. So constructors can't properly deal with them.
C++ gets around this by making you manually declare (and optionally initialize) any static member variables. Depending on where they are declared, they typically get constructed before your main() function starts, so they are available for use immediately.
I know that static members don't belong to objects.
But why is it so?
And when is the memory for static data members allocated?
I know that static members don't belong to objects. But why is it so?
Because you use static members whenever you have some quantity that all objects
"share", say for example the number of instances class Foo has. Such a "shared" member cannot belong to a particular instance. When a new Foo object is being created, then the static variable num_instances is incremented. If the variable wasn't static, then each new Foo will start with a clean state, and couldn't possibly know about other Foo's. Example:
#include <iostream>
class Foo
{
static int num_instances; // all instances share this variable
public:
Foo()
{
++num_instances; // increment the number of instances
}
static int get_num()
{
return num_instances;
}
};
int Foo::num_instances = 0;
int main()
{
Foo foo1, foo2;
std::cout << "We have " << Foo::get_num() << " Foo's" << std::endl;
}
And when is the memory for static data members allocated?
The memory for static objects is allocated before program startup.
When we declare a member variable static, it is shared between all instances of the class. I've heard that you should think of the variable belonging to the class itself, not any instance. This lets us initialize the variable without instantiating any object of the class, which makes sense.
class Something
{
public:
static int s_nValue;
};
int Something::s_nValue = 1;
But why are we allowed to initialize a private static member?
class Something
{
private:
static int s_nValue;
};
int Something::s_nValue = 1;
Does private even mean anything when we are talking about static members?
Yes, it does mean something. Consider the following example, which throws a compiler error, because the member is private. Being able to initialize a private variable is not the same as being able to change it from any context.
class Something
{
private:
static int s_nValue;
};
int Something::s_nValue = 1;
int main(){
Something::s_nValue = 2; // Compiler error here.
}
Private still means the same thing: you cannot use the name Something::s_nValue except in the definition of a member of Something (or a friend, or a nested class within Something).
int Something::s_nValue = 1;
is the definition of a member of Something - namely, that static member s_nValue.
int Something::another_static_val = s_nValue; // also okay
int OtherClass::x = Something::s_nValue; // Illegal access!
int Something::getValue() const {
return s_nValue; // okay, getValue is a member of same class
}
int regularFunction() {
return Something::s_nValue; // Illegal access!
}
Does private even mean anything when we are talking about static members?
I'll try to answer with a classic example. Consider the following piece of code:
#include <iostream>
class foo {
static int count;
int id;
public:
foo() : id(++count) {}
int getid() const { return id; }
};
int foo::count = 0;
int main() {
foo f1, f2, f3;
std::cout << f1.getid() << std::endl;
std::cout << f2.getid() << std::endl;
std::cout << f3.getid() << std::endl;
}
LIVE DEMO
In the example above we use a private static int to count the instances of foo created. We made the count static member variable private because we don't want anyone else except object of type foo to mess with it.
And this is only a naive example, think of the possibilities.
Public, private and protected are properties of a class and not of an object. Their purpose is to let you specify which parts of this class are visible to other classes, and not to hide stuff from objects of the same class. So, you can write code like this :
class A
{
public:
bool operator<(const A& other)
{
return this->val < other.val;
}
private:
int val;
};
So, private makes sense even when applied to static members - it just says that other classes cannot see this member.