Accessing private static variable outside class - c++

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.

Related

How is a static member in a class allocated?

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).

How static member function can call the static private data member ? what kind of the internal transformation is needed?

Just imagine we have the following class:
class A
{
private:
static int m_a;
public:
A() {}
static int get_sum(int b);
};
int A::m_a = 5;
int A::get_sum(int b)
{
return m_a + b;
}
int main() {
// your code goes here
A a;
int c = a.get_sum(10);
cout << "C=: " << c << endl;
return 0;
}
In the code above, we have class which contains one private, static member variable which called into our public, static member function get_sum(). Now the question: How the function which has not "this" pointer can access class member variable m_a ? In the Lipman's book I have read that:
(( Point3d* ) 0 )->object_count();
where object_count() does nothing more than return the _object_count
static data member. How did this idiom evolve ?
..............................
..............................
//internal transformation of call
object_count(( Point3d* ) 0 );
The language solution was the introduction of static member functions
within the official cfront Release 2.0. The primary characteristic of
a static member function is that it is without a this pointer.
I don't understand how we can cast 0 to class type object ?
Static member functions don't have this pointer and static data member is class-specific rather than object-specific. So, static member functions can access static member variables.

Private static class members

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.

How access class variables in c++

Is it possible in c++ to access class variables in other classes without creating an object. I have tried to use static, but the other class doesnt recognize my variable.
I have 3 classes. In two of those the sae variables should be used. In the third class I am changing the values. Would be grateful if you could help. Maybe youve got an example.
class Myclass
{
public:
static int i;
};
int Myclass::i = 10;
class YourClass
{
public:
void doSomething()
{
Myclass::i = 10; //This is how you access static member variables
}
};
int main()
{
YourClass obj;
obj.doSomething();
return 0;
}
static is the right keyword here:
class A {
public:
static int i; // <-- this is a class variable
};
class B {
public:
void f() { A::i = 3; } // <-- this is how you access class variables
};
They only potential problem I can think of is that
You made the class variable protected or private, thus rendering it inaccessible from other code.
You forgot to specify the full scope of the class variable (with A:: in this example).
I think the Singleton Pattern would help, but I'm no big fan of it. A lot better design would be to have one class take ownership of the object, and pass references to this object to the other classes.
yes you can bro, try this
struct car{
string model;
string paint;
int price;
};
int main(){
// creates an object of the class car
car BMW;
// assign the values
bmw.model = "m sports";
bmw.paint ="red";
bmw.price = 24000;
}

Can inner classes access private variables?

class Outer {
class Inner {
public:
Inner() {}
void func() ;
};
private:
static const char* const MYCONST;
int var;
};
void Outer::Inner::func() {
var = 1;
}
const char* const Outer::MYCONST = "myconst";
This errors out when I compile with class Outer::Inner' has no member named `var'
An inner class is a friend of the class it is defined within.
So, yes; an object of type Outer::Inner can access the member variable var of an object of type Outer.
Unlike Java though, there is no correlation between an object of type Outer::Inner and an object of the parent class. You have to make the parent child relationship manually.
#include <string>
#include <iostream>
class Outer
{
class Inner
{
public:
Inner(Outer& x): parent(x) {}
void func()
{
std::string a = "myconst1";
std::cout << parent.var << std::endl;
if (a == MYCONST)
{ std::cout << "string same" << std::endl;
}
else
{ std::cout << "string not same" << std::endl;
}
}
private:
Outer& parent;
};
public:
Outer()
:i(*this)
,var(4)
{}
Outer(Outer& other)
:i(other)
,var(22)
{}
void func()
{
i.func();
}
private:
static const char* const MYCONST;
Inner i;
int var;
};
const char* const Outer::MYCONST = "myconst";
int main()
{
Outer o1;
Outer o2(o1);
o1.func();
o2.func();
}
An inner class has access to all members of the outer class, but it does not have an implicit reference to a parent class instance (unlike some weirdness with Java). So if you pass a reference to the outer class to the inner class, it can reference anything in the outer class instance.
Anything that is part of Outer should have access to all of Outer's members, public or private.
Edit: your compiler is correct, var is not a member of Inner. But if you have a reference or pointer to an instance of Outer, it could access that.
First of all, you are trying to access non-static member var outside the class which is not allowed in C++.
Mark's answer is correct.
Anything that is part of Outer should have access to all of Outer's members, public or private.
So you can do two things, either declare var as static or use a reference of an instance of the outer class to access 'var' (because a friend class or function also needs reference to access private data).
Static var
Change var to static If you don't want var to be associated with the instances of the class.
#include <iostream>
class Outer {
private:
static const char* const MYCONST;
static int var;
public:
class Inner {
public:
Inner() {
Outer::var = 1;
}
void func() ;
};
};
int Outer::var = 0;
void Outer::Inner::func() {
std::cout << "var: "<< Outer::var;
}
int main() {
Outer outer;
Outer::Inner inner;
inner.func();
}
Output- var: 1
Non-static var
An object's reference is a must to access any non-static member variables.
#include <iostream>
class Outer {
private:
static const char* const MYCONST;
int var;
public:
class Inner {
public:
Inner(Outer &outer) {
outer.var = 1;
}
void func(const Outer &outer) ;
};
};
void Outer::Inner::func(const Outer &outer) {
std::cout << "var: "<< outer.var;
}
int main() {
Outer outer;
Outer::Inner inner(outer);
inner.func(outer);
}
Output- var: 1
Edit - External links are links to my Blog.
var is not a member of inner class.
To access var, a pointer or reference to an outer class instance should be used. e.g. pOuter->var will work if the inner class is a friend of outer, or, var is public, if one follows C++ standard strictly.
Some compilers treat inner classes as the friend of the outer, but some may not. See this document for IBM compiler:
"A nested class is declared within the scope of another class. The name of a nested class is local to its enclosing class. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables.
Member functions of a nested class follow regular access rules and have no special access privileges to members of their enclosing classes. Member functions of the enclosing class have no special access to members of a nested class."