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) {}
};
Related
I have a class that manages a map of strings and pointers as such:
class DebugTab
{
public:
void pushItem(std::string&& name, std::unique_ptr<DebugItem> item);
private:
std::map<std::string, std::unique_ptr<DebugItem>> items_;
};
The pointers should be able to point to different types so to achieve this I made this simple abstract class:
class DebugItem
{
public:
virtual std::string asString() = 0;
};
And then made a few derived classes for fundamental types:
class DebugInt : public DebugItem
{
public:
DebugInt(int& i) : i_(i) {}
std::string asString() override {return std::to_string(i_);}
private:
int& i_;
};
class DebugFloat : public DebugItem
{
public:
DebugFloat(float& f) : f_(f) {}
std::string asString() override {return std::to_string(f_);}
private:
float& f_;
};
class DebugString : public DebugItem
{
public:
DebugString(std::string& s) : s_(s) {}
std::string asString() override {return s_;}
private:
std::string& s_;
};
My idea was that the person using DebugTab would create the item with the correct class on the heap using 'new' and then pass the pointer to pushItem like this:
DebugTab tab;
int var1;
float var2;
std::string var3;
tab.pushItem("var1", std::move(std::make_unqiue<DebugInt>(var1)));
tab.pushItem("var2", std::move(std::make_unique<DebugFloat>(var2)));
tab.pushItem("var3", std::move(std::make_unique<DebugString>(var3)));
I'm not very satisfied with this solution. I feel that it just isn't very intuitive to pass a heap pointer to a class that tracks already created variables.
Is there a better, simpler way of doing all of this that comes to mind, or is the system I have now sufficient?
Well, technically you can skip allocating them at callsite...
template<class T>
struct DebugConcrete: DebugItem {
T &t;
std::string asSring() const override {
return std::to_string(t);
}
};
class DebugTab {
template<class T>
void pushItem(std::string name, T &item) {
items_.emplace(std::move(name),
std::make_unique<DebugConcrete<T>>(item));
}
};
#define pushItem(...) pushItem(#__VA_ARGS__, __VA_ARGS__)
int main() {
int i;
DebugTab tab;
tab.pushItem(i);
}
Defining the classes A with private constructor and destructor (it should be so!) and B as a friend class, how can I creat a vector of A objects in B and fill it with the function addA(). I got the error "error C2248: "A::~A": No access to private members whose declaration was made in the A class".
class A
{
private:
A();
A(const std::string& name, const float& num);
~A();
public:
friend class B;
private:
std::string name_;
float num_;
};
A::A()
{
name_ = "NoName";
num_ = 0.0;
}
A::A(const std::string& name, const float& num)
{
name_ = name;
num_ = num;
}
A::~A()
{
}
class B
{
public:
B();
~B();
void addA(const std::string name, const float num);
private:
vector<A> vecA;
};
B::B()
{
}
B::~B()
{
}
void B::addA(const std::string name, const float num)
{
A a(name, num);
vecA.push_back(a);
}
int main()
{
B b;
b.addA("Name", 1.0);
return 0;
}
While #Fureeish has a neat solution, here's a slightly simpler alternative: just wrap it.
class AccessPrivate;
class PrivStuff
{
private:
PrivStuff() {}
~PrivStuff() {}
public:
friend class AccessPrivate;
std::string m_data{};
};
class AccessPrivate
{
public:
AccessPrivate() = default;
~AccessPrivate() = default;
PrivStuff m_priv;
};
int main(int argc, char* argv[])
{
std::vector<AccessPrivate> myvec;
myvec.resize(4);
for (auto& stuff : myvec)
{
stuff.m_priv.m_data = "heya";
}
}
If you need something more complicated, like passing in arguments, just add an equivalent constructor to AccessPrivate and there you go. You can essentially treat AccessPrivate almost like the actual private class, just one level of indirection.
how can I create a vector of A objects in B [...] ?
You can't do that. While B is a friend of A, std::vector is not a friend of A, which means that it cannot access private members of A, e.g., constructor, which is required for a vector to work.
However, if you are okay with a little indirection, little potential performance hit and a change in your signature, you can replace the not-working std::vector<A> with a workig std::vector<std::unique_ptr<A, deleter>>.
It's important to note that plain std::unique_ptr will not work here. It has a similar problem to std::vector - it cannot access private destructor of A. One way to work around it is to outsource the job of constructing and destructing of As entirely to B - via explicit construction and destruction, that is:
new A(name, num)
static void deleter_a(A* a) { delete a; }
in B's scope.
Now we can do:
std::vector<std::unique_ptr<A, std::function<void(A*)>>> vecA;
instead of: std::vector<A> or std::vector<std::unique_ptr<A>>. This is important - neither std::unique_ptr nor std::vector construct or destruct your As. B is entirely responsible for constructing (new A(name, num)) and destructing (static void deleter_a(A* a) { delete a; }) As.
Full B class:
class B {
public:
B() {}; // or = default
~B() {}; // or = default
void addA(const std::string name, const float num);
private:
static void deleter_a(A* a) { delete a; }
using deleter_a_t = void(A*);
std::vector<std::unique_ptr<A, std::function<deleter_a_t>>> vecA;
};
void B::addA(const std::string name, const float num) {
vecA.push_back(std::unique_ptr<A, std::function<deleter_a_t>>{
new A(name, num), std::function<deleter_a_t>{deleter_a}
});
}
Contrary to what the other answers say, it is possible to do this without any extra indirection.
std::vector doesn't directly call the constructor and the destructor, but uses an allocator. If you want an std::vector to manage A objects, you just need to provide it an allocator that implements the construct and destroy functions, and that is either a friend of A or a nested class of B (since B is already a friend of A).
Example:
#include <memory>
#include <utility>
#include <vector>
class A {
A() = default;
~A() = default;
friend class B;
};
class B {
template<typename T>
struct custom_alloc : std::allocator<T> {
template<typename U, typename... Args>
void construct(U* p, Args&&... args){
::new(const_cast<void*>(static_cast<const volatile void*>(p))) U(std::forward<Args>(args)...);
}
template<typename U>
void destroy(U* p){
if constexpr (std::is_array_v<U>){
for(auto& elem : *p){
(destroy)(std::addressof(elem));
}
} else {
p->~U();
}
}
};
public:
std::vector<A,custom_alloc<A>> vec;
void new_A(){
vec.push_back(A());
}
};
For the implementation of construct and destroy, I used an equivalent implementation of the c++20 versions of std::destroy_at and std::construct_at. I suspect that destroy is overkill and just a call to the destructor would be sufficient, but I'm not sure.
I want to set a reference after constructor
Example:
class OtherClass
{
public:
OtherClass() : m_class(Class()){}
inline void SetData(int data1, int data2)
{
//calculate data3
// I tried:
m_class = Class(data3);
//but it doesn't worked
}
protected:
private:
Class& m_class;
};
Edit:
The Exception is: vector subscript out of range
because I have glm vectors in the Class.
I need also call functions in my Class.
Edit 2:
Why I need this?
Because I have an other class [ExClass] which extends and which have to calulate in constructor:
ExClass::ExClass(float d1, float d2, ...) {
//calculate data from given values
SetData(data);
}
The proper way to do that is to use a pointer and not a reference, as opposed to references - pointers can be set after object creation. Note also that referring (or pointing) to a local variable whose lifetime will end, while still in use, is a bad idea.
Your code may be changed to use a pointer and dynamic allocation or alternatively, std::unique_ptr. There are of course other options, these are just examples.
Option 1 - a pointer and dynamic allocation
class OtherClass
{
public:
OtherClass() : m_class(nullptr){}
~OtherClass() {
delete m_class;
}
// block copy and assignment (or implement them)
OtherClass(const OtherClass&) = delete;
OtherClass& operator=(const OtherClass&) = delete;
void setData(int data1, int data2)
{
// ... calculate data3 ...
m_class = new Class(data3);
}
bool hasInnerObj() const {
return m_class; // or: return m_class != nullptr;
}
/** call this function only if hasInnerObj() returned true */
Class& getInnerObj() {
return *m_class;
}
private:
Class* m_class;
};
Option 2 - std::unique_ptr
class OtherClass
{
public:
void setData(int data1, int data2)
{
// ... calculate data3 ...
m_class = std::make_unique<Class>(data3);
}
bool hasInnerObj() const {
return m_class; // or: return m_class != nullptr;
}
/** call this function only if hasInnerObj() returned true */
Class& getInnerObj() {
return *m_class;
}
private:
std::unique_ptr<Class> m_class;
};
You have two problems:
Reference class members (i.e. m_class) need to be initialized when object is created.
However, both your Class instances (one in the constructor, and one in SetData) are put on the stack and popped right away, making the reference invalid.
What you need to do is make sure that your class object actually lives through the function call. One way of achieving that is allocating it prior to passing it to the OtherClass constructor or SetData function:
class Class {};
class OtherClass
{
public:
OtherClass(Class& c) : m_class(c){}
inline void SetData(Class& c)
{
m_class = c;
}
protected:
private:
Class& m_class;
};
int main()
{
Class a;
OtherClass c(a);
Class b;
c.SetData(b); // changes m_class
return 0;
}
Live example here.
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 ;
}
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)
{
}
};