Question: how to access the fields of the structure?
class A { private: struct B { std::string W; }; BD[100]; public: A(){}; void addW(std::string W); };
Question: how to access the fields of the structure?
class A { private: struct B { std::string W; }; BD[100]; public: A(){}; void addW(std::string W); };
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
For example:
class A { private: struct B { std::string W; }; BD[100]; int last = 0; public: A(){}; void addW(std::string W) { if (last == 100) throw exception("Выход за пределы диапазона") D[last++].W = W; } };
Inside a class, you can do whatever you want with private
. If the question of how to address it from the outside is, for example, like this:
public: A(){}; void addW(std::string W) { D[last++].W = W; } std::string get(int index) const { // Проверка индекса на корректность return D[index].W; }
Maybe you should consider using the std::vector
container?
#include <vector> class A { private: struct structB { std::string field; }; std::vector<structB> arrayOfB; public: A() {}; void addString(const std::string& str) { structB appenededB; appendedB.field = str; arrayOfB.push_back(appendedB); } };
Source: https://ru.stackoverflow.com/questions/507317/
All Articles
D[index].W = W;
. The abundance of upper case is confusing. - KoVadim