I have tried the following:

struct X{
string s[3];
.............
X(): s[0]("a"), s[1]("b"), s[2]("c") {}; // failes: C2059
};

1. I hope it is clear that s is an array of 3 strings and not a string of 3 chars.
2. How to init such an array.

Thanks,
David

Re: How to init array of strings member by a constructor? by David

David
Sun Oct 22 17:56:58 CDT 2006

I forgot to mention that:

3. If I don't initialize s in a constructor but then in the code write:
void main(){
X y;
y.s[0]="abc";
y.s[1]="bcd";
y.s[2]="cde";
...............
}

everything seems to be fine.

David

"David F" <David-White@earthlink.net> wrote in message
news:b9S_g.11024$Lv3.8940@newsread1.news.pas.earthlink.net...
> I have tried the following:
>
> struct X{
> string s[3];
> .............
> X(): s[0]("a"), s[1]("b"), s[2]("c") {}; // failes: C2059
> };
>
> 1. I hope it is clear that s is an array of 3 strings and not a string of 3 chars.
> 2. How to init such an array.
>
> Thanks,
> David
>
>



Re: How to init array of strings member by a constructor? by Alex

Alex
Sun Oct 22 18:33:57 CDT 2006

"David F" wrote:
> I have tried the following:
>
> struct X{
> string s[3];
> .............
> X(): s[0]("a"), s[1]("b"), s[2]("c") {}; //
> failes: C2059
> };
>
> 1. I hope it is clear that s is an array of 3 strings and
> not a string of 3 chars.
> 2. How to init such an array.


X::X()
{
s[0] = "a";
s[1] = "b";
s[2] = "c";
}



Re: How to init array of strings member by a constructor? by David

David
Sun Oct 22 19:04:20 CDT 2006

Thanks. I thought so too and tried it to be ok.
But still, what is wrong with the initialization list style?
Is it my syntax or just not possible?
If its the latter, it seems to be some inconsistency in
the laguage definition or its implementation. Isn't it?

David

"Alex Blekhman" <xfkt@oohay.moc> wrote in message news:uEmIJKj9GHA.3740@TK2MSFTNGP05.phx.gbl...
> "David F" wrote:
> > I have tried the following:
> >
> > struct X{
> > string s[3];
> > .............
> > X(): s[0]("a"), s[1]("b"), s[2]("c") {}; //
> > failes: C2059
> > };
> >
> > 1. I hope it is clear that s is an array of 3 strings and
> > not a string of 3 chars.
> > 2. How to init such an array.
>
>
> X::X()
> {
> s[0] = "a";
> s[1] = "b";
> s[2] = "c";
> }
>
>



Re: How to init array of strings member by a constructor? by John

John
Sun Oct 22 20:57:17 CDT 2006

"David F" <David-White@earthlink.net> wrote in message
news:8GT_g.11073$Lv3.10522@newsread1.news.pas.earthlink.net
> Thanks. I thought so too and tried it to be ok.
> But still, what is wrong with the initialization list style?
> Is it my syntax or just not possible?
> If its the latter, it seems to be some inconsistency in
> the laguage definition or its implementation. Isn't it?

It is just not possible. Arrays are part of C and don't play well with
constructors.

--
John Carson



Re: How to init array of strings member by a constructor? by Tom

Tom
Mon Oct 23 09:00:44 CDT 2006

David F wrote:
> Thanks. I thought so too and tried it to be ok.
> But still, what is wrong with the initialization list style?
> Is it my syntax or just not possible?
> If its the latter, it seems to be some inconsistency in
> the laguage definition or its implementation. Isn't it?

Right. It's going to be fixed in the next version of C++, hopefully:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2100.pdf

Tom

Re: How to init array of strings member by a constructor? by David

David
Sat Oct 28 17:48:48 CDT 2006

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.

I complitly forgot about it and should have rechecked
before posting the quetion. Sorry.

David

"Alex Blekhman" <xfkt@oohay.moc> wrote in message news:uEmIJKj9GHA.3740@TK2MSFTNGP05.phx.gbl...
> "David F" wrote:
> > I have tried the following:
> >
> > struct X{
> > string s[3];
> > .............
> > X(): s[0]("a"), s[1]("b"), s[2]("c") {}; //
> > failes: C2059
> > };
> >
> > 1. I hope it is clear that s is an array of 3 strings and
> > not a string of 3 chars.
> > 2. How to init such an array.
>
>
> X::X()
> {
> s[0] = "a";
> s[1] = "b";
> s[2] = "c";
> }
>
>



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