How to access a static member without having an object (C++) - c++

I have two classes, the first one has a static member variable. I want to get the value of that variable in a function of the other class but without passing an object of the first class. So how can I call the get-function of the first class? Example:
class c1
{
private:
static int val;
public:
int getVal();
};
int c1::val = 0;
c1::getVal()
{
return val;
}
How can I access val without passing an object of c1 to doSomething()?
class c2
{
public:
void doSomething();
};
c2::doSomething()
{
int someCalculation = getVal(); //doesn't work
...
}

Try this...
..in c1...
static int getVal()
.. in c2...
c2::doSomething()
{
int someCalculation = c1::getVal();
...
}

Related

How to declare a member variable that is only visible to one class method per class instance?

Is there a way to declare member4 that only visible to Function1 and it is not shared by all instances?
class Test
{
public:
void Function1()
{
???? int member4 //visble to Function1 (single instance)
}
void Function2()
{
static int member3;// visble to Function2 (all instances)
}
private:
int member1; // visble to Function1 and Function2 (single instance)
static int member2;//visble to Function1 and Function2 (all instances)
};
Your question looks like an XY Problem. Anyway, here are two possible solutions.
First, you could wrap the field into a class and declare the method as a friend:
class Test {
public:
// Don't forget to init Data:
Test();
void function1();
void function2();
private:
class Data;
Data *data;
};
class Test::Data {
int get() { return 42; }
friend void Test::function1();
};
void Test::function1() {
int secretData = data->get();
}
void Test::function2() {
// Will not compile:
// int secretData = data->get();
}
This solution smells. Much better would be to extract the entity that has this secret member into a separate class:
class AnotherEntity {
public:
void function1() {
// use secretData here;
}
private:
int secretData;
};
class Test : public AnotherEntity {
public:
void function2();
private:
};

C++: is it possible to late-initialize a class member function?

I have the following code:
#include <iostream>
class First {
public:
Second* x;
void make_value(Second* sec_);
First() {
// Initialization
}
};
class Second {
public:
First* y;
Second() {
// Initialization
}
};
void First::make_value(Second* sec_) {
x = sec_;
}
void main() {
fir = new First();
sec = new Second();
fir->make_value(sec);
}
The two classes each have a member variable of the other class, which does not work for obvious reasons.
My question is whether or not there is a way to late-initialize variable x after class Second has been initialized. If not, what alternatives are there?
For any uses where the compiler doesn't need the definition of a class, a forward declaration will suffice. Pointers and references to types do not require a definition.
class Second; // forward declare
class First {
public:
Second* x;
void make_value(Second* sec_);
First() {
// Initialization
}
};
class Second {
public:
First* y;
Second() {
// Initialization
}
};
void First::make_value(Second* sec_) {
x = sec_;
}

C++: How to write from a static function to a global variable

I want to access a variable (through ctypes) which is supposed to be the result of a static void function, that is listening to broadcast.
So how do I get information out of a static function except using the "return" statement?
edit:
Here is an example code of what I mean:
class Foo{
int bar;
static void listener(){
bar = 3;
}
main(){
listener();
}
}
static method can only access static members:
class Foo {
static int bar;
public:
static void listener() { bar = 3; }
};
int Foo::bar = 0;
int main()
{
Foo::listener();
}
You can declare your variable as static:
class Foo {
public:
static int bar;
static void listener() {
bar = 3;
}
};
int Foo::bar = 0;
main() {
Foo::listener();
}
// now use Foo::bar. It's value is 3.
}
Heads up that the variable being public and static can be modified by everyone, not only by Foo::listener

Using a static const member from static non-member function

I have a private static const member in class, and in the class implementation I have static function that tries to use this const, but it gives me errors.
//A.hpp
class A {
static const int X = 1; //<<problem here
// ....
}
and I have
//A.cpp
static int DoSomething();
// ....
static int DoSomething {
int n = A::X; //<<problem here
//....
}
and I get within this context when I try to use X from DoSomething and ‘const int A::X’ is private in the static const int X = 1;.
How can I fix that?
You are trying to access a private member of A from a free function. This is not allowed.
You should make it public, eg:
class A {
public:
static const int X = 1;
}
An alternative solution to Jack's answer is to make the function DoSomething() non-static and declare it as a friend of class A:
//A.hpp
class A {
static const int X = 1;
// ....
friend int DoSomething();
};
//A.cpp
int DoSomething() {
int n = A::X;
//....
}

How to declare two different static variables? (C++)

EDIT: declaring them private was a typo, I fixed it:
Relating to another question, if I declared a static variable in a class, then derived a class from that, is there any way to declare the static variable as individual per each class. Ie:
class A:
{
public:
static int x;
};
class B:A
{
public:
const static int x;
};
does that define TWO DIFFERENT static variables x, one for A and one for B, or will I get an error for redefining x, and if I do get an error, how do a I create two seperate static variables?
When you're using static variables, it might be a good idea to refer to them explicitly:
public class B:A
{
public const static int x;
public int foo()
{
return B::x;
}
}
That way, even if the class "above" yours in the hierarchy decides to create a similarly-named member, it won't break your code. Likewise, I usually try to us the this keyword when accessing normal member fields.
Updated to use C++ syntax.
That creates two separate static variables.
Note that you have implicitly declared these private:
class A:
{
private:
static int x;
};
class B:A
{
private:
const static int x;
};
Which means the variables are not in competition with each other.
As already said, that creates two separate variables :
A::x;
// and
B::x;
In fact if you try to use just 'x' in a method of B, only the closer scope definition will be used until you are more precise :
#include <iostream>
class A
{
protected:
static int x;
public:
A() { x = 7; }
};
int A::x = 22;
class B:A
{
static const int x = 42;
public:
int a_x() const { return A::x; }
int b_x() const { return B::x; }
int my_x() const { return x; } // here we get the more local variable, that is B::x.
};
int main()
{
B b;
std::cout << "A::x = " << b.a_x() << std::endl;
std::cout << "B::x = " << b.b_x() << std::endl;
std::cout << "b's x = " << b.my_x() << std::endl;
std::cin.ignore();
return 0;
}
Outputs:
A::x = 7
B::x = 42
b's x = 42
Someone mentioned that accessibility might limit accessibility : making the base variable private will not make it accessible to the child class.
However, if the variable have to be protected or public, use an explicit access method or rely on the local scope rule I just demonstrated.
If you want a static variable that is individual to each class that uses A - you can use a template class.
For e.g.
template<class T> struct A
{
A() { ++s_var; }
static int s_var;
};
template<class T> int A<T>::s_var;
stuct B :A<B> {
B() { ++s_var; } // this is a different s_var than the one used by 'C' below
};
struct C : A<C> {
C() { ++s_var; } // this is a different s_var than the one used by 'B'
};