Re: How to init array of strings member by a constructor? by Alex
Alex
Sun Oct 29 06:05:00 CST 2006
David F wrote:
> I have recalled in the meanwhile that it has nothing to
> do with arrays of strings but it has to do with any array,
> and more precisely with any aggregate member.
> It is by the language definition as mention in the TC++PL book.
> Since initialization is not the same as assignment - an
> explicit constructor's initialization list can only initialized
> what the default constructor can and it has to be an entire member.
> Hence, can't init individual elements of a vector for example.
> In the way you described, which is like statements of any
> function, you can issue ASSIGNMENT statement which
> can be applied as usual.
Yes, you're right. Initialization of aggregates is performed
by copy initialization and equivalent to the form:
T var = value;
So, strictly speaking, following initialization:
std::string arr[3] = { "a", "b", "c" };
should be performed via temporary copy (in pseudocode):
std::string tmpA("a");
std::string tmpB("b");
std::string tmpC("c");
std::string* arr = malloc(sizeof(std::string) * 3);
std::string::string(arr + 0, tmpA);
std::string::string(arr + 1, tmpB);
std::string::string(arr + 2, tmpC);
However, in the case above compiler is allowed to eliminate
redundant temporary copy and construct target object from
initial value directly.
HTH
Alex