Using a static const member from static non-member function - c++

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;
//....
}

Related

Calling and initializing static member function of class

I have the following code:
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
class A {
public:
int f();
int (A::*x)();
};
int A::f() {
return 1;
}
int main() {
A a;
a.x = &A::f;
printf("%d\n",(a.*(a.x))());
}
Where I can initialize the function pointer correctly. But I want to make the function pointer as static, I want to maintain single copy of this across all objects of this class.
When I declare it as static
class A {
public:
int f();
static int (A::*x)();
};
I am unsure of the way/syntax to initialize it to function f. Any resource would be helpful
A static pointer-to-member-function (I guess you already know this is different from a pointer to a static member function) is a kind of static member data, so you have to provide a definition outside the class like you would do with other static member data.
class A
{
public:
int f();
static int (A::*x)();
};
// readable version
using ptr_to_A_memfn = int (A::*)(void);
ptr_to_A_memfn A::x = &A::f;
// single-line version
int (A::* A::x)(void) = &A::f;
int main()
{
A a;
printf("%d\n",(a.*(A::x))());
}

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

How to access a static member without having an object (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();
...
}

How to call the constructor when all the methods in a class are static?

https://stackoverflow.com/a/8839647/462608
Use the static initializer:
public class MyClass
{
static {
//init
}
}
Can something similar be done in C++?
Actually, I need to initialize some variables, but I don't want to create an object.
If the variables are static members, not only are you able to initialize them, but you must initialize them.
There's no direct equivalent of Java initializer lists, but something similar can be done calling a function to initialize a static member:
class X
{
static bool x;
}
bool foo()
{
//initialization code here
}
bool X::x = foo();
This is for cases with intense logic. If all you want is to initialize static members, just do it similar to X::x.
Actually, I need to initialize some variables, but I don't want to create an object.
If the variables are outside the class, initialize them directly (don't need calling code for that).
If the variables are static members of the class, use one of the above approaches.
If the variables are non-static members, they simply don't exist without an object.
Static variables of a class are initialized in .cpp file:
MyClass.h
class MyClass {
public:
static void f1();
static void f2();
private:
static int a1;
static int a2;
static int a3;
};
MyClass.cpp
int MyClass::a1 = 7;
int MyClass::a2 = 8;
int MyClass::a3 = MyClass::a1 + MyClass::a2;
void MyClass::f1()
{
a1 = 7;
}
void MyClass::f2()
{
cout << a2 << endl;
}
If you want non trivial initialization of static member variables - group them in inner class with constructor:
MyClass.h
class MyClass {
public:
static void f1();
static void f2();
private:
struct Data {
Data();
int a1;
int a2;
int a3;
};
static Data data;
};
MyClass.cpp
MyClass::Data MyClass::data;
MyClass::Data::Data() : a1(0), a2(0), a3(0)
{
// it is just an example ;)
for (int i = 0; i < 7; ++i)
{
a1 += 2;
s2 += 3;
a3 += a1 + a2;
}
}
void MyClass::f1()
{
data.a1 = 7;
}
void MyClass::f2()
{
cout << data.a2 << endl;
}

Can i declare member variable as const in class of c++?if yes,how?

Can i declare member variable as const in class of c++?if yes,how?
You can - you put const in front of the type name.
class C
{
const int x;
public:
C() : x (5) { }
};
You declare it as you would if it wasn't a member. Note that declaring a variable as const will have considerable effects on the way that the class is used. You will definitely need a constructor to initialise it:
class A {
public:
A( int x ) : cvar( x ) {}
private:
const int cvar;
};
Sure, the simplest way is like this if the value will be the same across all instances of your class:
class X
{
public:
static const int i = 1;
};
Or if you don't want it static:
class X
{
public:
const int i;
X(int the_i) : i(the_i)
{
}
};