Static const member initialization outside class - c++

how can i initialize static const int member outside class ?
class C
{
public:
C();
private:
static const int a;
static const std::string b;
};
// const int C::a = 4; // i know it
// const std::string C::b = "ba"; // this one too

I'd go with C++17 and use inline static object:
class C
{
public:
C() = default;
private:
inline static const int a = 42;
inline static const std::string b{"foo"};
};
Easiest, cleaniest, most readable.

This is how to initialize static class members outside of the class:
C.cpp
#include <string>
class C {
public:
C();
private:
static const int a;
static const std::string b;
};
C::C() = default;
const int C::a = 4;
const std::string C::b = "ba";
There is no other option to initialize a static class member outside of the class, other than all the various C++ ways to do initialization as variations on this same theme.

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))());
}

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

c++ class constant of its own class

Basically, I would like to declare constants of a class within the class itself:
class MyClass {
int itsValue;
public:
MyClass( int anInt) : itsValue( anInt) {}
static const MyClass CLASSCONST;
};
So I can access it like this;
MyClass myVar = MyClass::CLASSCONST;
But I can't find a way to initialize MyClass::CLASSCONST. It should be initilized inside the MyClass declaration, but at that point the constructor is not known. Any one knowing the trick or is it impossible in c++.
class MyClass {
int itsValue;
public:
MyClass( int anInt) : itsValue( anInt) {}
static const MyClass CLASSCONST;
};
const MyClass MyClass::CLASSCONST(42);
Here is a working example with definition outside the class.
The class declaration has a const static member which is initialized outside the class as it is a static member and of type non-integral. So initialization inside the class itself is not possible.
#include <iostream>
class test
{
int member ;
public:
test(int m) : member{m} {}
const static test ob ;
friend std::ostream& operator<<(std::ostream& o, const test& t)
{
o << t.member ;
return o;
}
};
const test test::ob{2};
int main()
{
std::cout << test::ob ;
}

const initialization of several data members

I would like to have a C++ class that gets initialized from a file that contains a bunch of data, reads the data, and stores it as const data members.
What I currently do is
MyClass(const std::string & fileName):
datum0(),
datum1(),
datum2()
{
this->read(fileName);
// read() sets all datumX data members.
}
This has the disadvantage that the datumXs cannot be marked const anymore since they are set up after the actual initialization step.
What would be a good pattern here?
Separate parsing and construction:
struct MyClass
{
int const a;
int const b;
MyClass(int a_, int b_) : a(a_), b(b_) { }
};
MyClass readMyClass(std::istream & is)
{
int a, b;
// ...
return MyClass(a, b);
}
Now you can say:
std::ifstream is("data.bin");
MyClass mc = readMyClass(is);
You can also make the reader function a static class member function and the constructor private if you prefer.
Instead of set of datumX members - use struct with these members which have constructor from file. And make this struct const data member of your class:
class MyClass {
...
struct MyData {
MyData(const string& filename) { read(filename); }
void read(const string& filename);
int datum1;
int datum2;
...
};
const MyData myData;
MyClass(const string& filename) : myData(filename) {}
};

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)
{
}
};