Initializing structs in C++ -


As an addendum, what's happening here:

  #include & lt ; String & gt; using namespace std; Structure A {String S; }; Int main () {A a = {0}; }  

Obviously, you can not set std :: string to zero. Provides an explanation about whether someone is actually going to be here (supported in context of C ++ standard) and then explain for example):

  int main () {Aa = {42}; }  

Is any of these well-defined?

Once again a shameful question for me - I always give my Constructors Constructor, Itemprop = "text">

Your Structure A Total , therefore the general rule of total initial work for him. The procedure is described in 8.5.1. Actually the entire 8.5.1 is dedicated to it, so I do not have a reason to see the copy of the whole thing here. The general idea is almost the same as that which was in C, just suited for C ++: You take a beginner with the right, you take a member on the left and you start the member with that beginner. According to 8.5 / 12, this would be an copy-initialization .

When you do

  a = {0};  

You are basically starting a copy of 0 with as , that is, as as < / Code> it is equivalent to semantics

  string s = 0;  

Compiled above because std :: string is a convertible from a const char * indicator (and it is undefined behavior, because the null pointer There is no valid argument in this case.)

Your 42 version will not be compiled for very similar reason

  string s = 42;  

will not compile 42 a pointless indicator is not constant, and std :: string to int There is no point for conversion from type.

PS Just in case: Note that the definition of total in C ++ is not recursive (for example, unlike POD definition) . std :: string is not an aggregate, but it does not change anything for your A . A is still a composite.


Comments