Home > Back-end >  C about Struct subclasses initialization problem
C about Struct subclasses initialization problem

Time:09-25

 


//the first way

Using Sub=struct _Sub {
STD: : string id;
STD: : string name;
};
Sub instance {" a ", "b"};//normal

//second way

Using the Base=struct _Base {
STD: : string id;
};
Using Sub1=struct _Sub1: public _Base {
STD: : string name;
};
Using Sub2=struct _Sub2: public _Base {
STD: : string name;
Bool load;
};
Sub1 instance {" a ", "b"};//an error, there is no suitable constructor

Class hierarchy is the second way, using structure to initialize the will to find the way to create an instance of the constructor of error, need to add the appropriate constructor in the class declaration,
Is there any way you can need not custom constructor, just like structure initialization?

CodePudding user response:

No way!
Why not define a constructor??????

Cannot initialize, can assign a value such as this
 
Sub1 instance;
The instance. The id="a";
The instance. The name="b";

CodePudding user response:

reference 1st floor wsang_web response:
no way!
Why not define a constructor??????

Cannot initialize, can assign a value such as this
 
Sub1 instance;
The instance. The id="a";
The instance. The name="b";


I only write these two member variables, there are actually a heap of member variables, if the constructor defined again want to write a lot of, what copy=operator function structure, consider just headache,
Or directly with the method of the assignment

CodePudding user response:

I understand it, when a class has no constructor, without inheritance, not virtual functions, then its memory layout is equal to c struct
So it can directly use {... }; Way to determine, he may also directly use memcpy, malloc, etc to initialize,,,
With inheritance, his memory layout is different from c struct, so you need to the constructor,,,,,,,,

CodePudding user response:

reference 3 floor truth is right or wrong response:
I understand it, when a class has no constructor, without inheritance, not virtual functions, then its memory layout is equal to c struct
So it can directly use {... }; Way to determine, he may also directly use memcpy, malloc, etc to initialize,,,
With inheritance, his memory layout is different from c struct, so you need to the constructor,,,,,,,,


Reasonable,,,

Have any master disassembly seen both cases struct memory layout is how



CodePudding user response:

Baidu Pod memory layout

CodePudding user response:

refer to the second floor mountain giant guo's reply:
Quote: refer to 1st floor wsang_web response:

No way!
Why not define a constructor??????

Cannot initialize, can assign a value such as this
 
Sub1 instance;
The instance. The id="a";
The instance. The name="b";


I only write these two member variables, there are actually a heap of member variables, if the constructor defined again want to write a lot of, what copy=operator function structure, consider just headache,
Or directly with the method of assignment



The role of inheritance is what? Is one of the role of the inheritance of serving the polymorphism,
Then you use inheritance significance is here???????
Do not directly define a structure of sweet?

CodePudding user response:

refer to 6th floor wsang_web response:
Quote: refer to the second floor mountain giant guo's reply:

Quote: refer to 1st floor wsang_web response:

No way!
Why not define a constructor??????

Cannot initialize, can assign a value such as this
 
Sub1 instance;
The instance. The id="a";
The instance. The name="b";


I only write these two member variables, there are actually a heap of member variables, if the constructor defined again want to write a lot of, what copy=operator function structure, consider just headache,
Or directly with the method of assignment



The role of inheritance is what? Is one of the role of the inheritance of serving the polymorphism,
Then you use inheritance significance is here???????
Do not directly define a structure of sweet?


Ask questions just simplified the scene, must be used in practical projects on polymorphism

CodePudding user response:

Why not write a constructor?
With constructor is good ah,
Said again, need not be defined copy assignment operator, it is you don't have the definition of an automatic synthesis

CodePudding user response:

Is not so much why, only rules ~ ~ ~

//the first way
Using Sub=struct _Sub {
STD: : string id;
STD: : string name;
};
Sub instance {" a ", "b"};//normal
Sub is clearly aggregated class, so the aggregated initialization,
===========================

//second way
Using Sub1=struct _Sub1: public _Base {
STD: : string name; };
Sub1 instance {" a ", "b"};//an error, there is no suitable constructor
Sub1 not aggregation class, so have to call the constructor,

As your wish, in c + + 17, aggregation class standard adjustments, Sub1 is aggregated class now, so Sub1 instance {" a ", "b"}; It won't be an error,
  • Related