How to correctly declare the structure that will be used in the class? All in one .h file like this:

struct Tdata{ std::string name; std::string second_name; //.... }; class A{ private: Tdata data; //... }; 

or so:

 class A{ private: struct Tdata{ std::string name; std::string second_name; //.... } data; //... }; 

or even spread the announcement of the structure and class of different files?

  • one
    both ways are correct. - Abyx

2 answers 2

If a structure is used only inside a class, and especially as a private member, then of course it is better to encapsulate its declaration into a class definition.

If you want to use a structure besides class definition, then it is better to declare it separately from the class.

Note that inside a class the structure declaration itself may have a public access class, whereas a data member of a class with this type may have a private access class.

 class A{ public: struct Tdata{ std::string name; std::string second_name; //.... }; private: Tdata data; //... }; 

It all depends on how you intend to use the class and structure, what interface you want to provide to users of the class and structure.

In addition, if the definition of a nested structure itself is not used in a class, then the structure can be defined outside the class, and in the class itself only to declare it. for example

 class A{ public: struct TData; private: Tdata *data; //... }; struct A::Tdata{ std::string name; std::string second_name; //.... }; 
  • I ran into an interesting bug, if you declare a pointer to a structure inside a class (Tdata * data;), then the program crashes with an error, and only the MinGW compiler code. The second compiler (Cygwin) creates a completely working program - xttz
  • @xttz To say something about this, you need to at least know the error message issued by the compiler. - Vlad from Moscow
  • The compiler works without errors and warnings, the application itself crashes after launching "Process returned -1073741819 (0xC0000005) execution time: 2.918 s" Wednesday CodeBlocks - xttz
  • @xttz I think this is due to some kind of bug in your program, and it has undefined behavior. - Vlad from Moscow
  • Yes, there is nothing else in this program, just started collecting everything in a heap, cloud.mail.ru/public/M2Bu/cooqdJ5VX and immediately ran into it - xttz

I used this method here:

 class Core { public: Core(); virtual ~Core(); struct InData; protected: private: InData *stData; }; struct Core::InData { int a; int b; }; 

after using one of the class methods

 stData->a = 5; 

program crashes after launch

  • I understand correctly that I need to add this to the class constructor: stData = new InData; - xttz
  • The reason is most likely that you did not initialize the stData pointer. It must point to a real object. For example, in the constructor you could add the stData = new inData () clause; - Vlad from Moscow
  • It’s just amazing that the cygwin compiler works and it doesn’t care about it, even without initialization, the pointer refers to some memory address, and not NULL. - xttz
  • If the pointer has not been initialized, then it can contain any random value. In this case, the behavior of the program is undefined. - Vlad from Moscow