Is init done for arrays in classes?

A

Alexander Malkis

class B {
//...
public:
B() { /* ... */};
};

class A {
B b[100];
public:
A() { };
//...
};

Question: are the elements of b get initialized during the call
A();

Thanks in advance
Alex.

PS. To email me, remove "loeschedies" from the email address given.
 
L

Leor Zolman

class B {
//...
public:
B() { /* ... */};
};

class A {
B b[100];
public:
A() { };
//...
};

Question: are the elements of b get initialized during the call
A();

Thanks in advance
Alex.

Slight rewrite:

#include <iostream>
using namespace std;

class B {
//...
public:
B() { std::cout << "B() "; }
};

class A {
B b[100];
public:
A() { std::cout << "A()"; }
//...
};

int main()
{
A a;
return 0;
}


Output:

B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B()
B() B() B() B() B()
B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B()
B() B() B() B() B()
B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B()
B() B() B() B() B()
B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B() B()
B() B() B() B() B()
B() B() B() B() A()

Answer your question? ;-)
-leor
 
A

Andrey Tarasevich

Alexander said:
class B {
//...
public:
B() { /* ... */};

The last semicolon is superfluous, even though it is not an error.
};

class A {
B b[100];
public:
A() { };

Same here.
//...
};

Question: are the elements of b get initialized during the call
A();

Yes. (Assuming that I understand correctly what you mean under "the call
A();")
 
V

Victor Bazarov

Alexander said:
class B {
//...
public:
B() { /* ... */};
};

class A {
B b[100];
public:
A() { };
//...
};

Question: are the elements of b get initialized during the call
A();

12.6.2/4 says that since 'B' is a non-POD class, it's default-initialised.

V
 
V

Victor Bazarov

Leor said:
On Tue, 18 May 2004 18:50:36 +0200, Alexander Malkis


[..code..]
Output:
[..output..]

Answer your question? ;-)

Leor, usually an example of a program made with a compiler is not enough
to judge the rest of the compilers, and the language Standard. Is that
why you included a smiley after your post, since it's kind of a joke?

V
 
L

Leor Zolman

Leor said:
On Tue, 18 May 2004 18:50:36 +0200, Alexander Malkis


[..code..]
Output:
[..output..]

Answer your question? ;-)

Leor, usually an example of a program made with a compiler is not enough
to judge the rest of the compilers, and the language Standard. Is that
why you included a smiley after your post, since it's kind of a joke?

No joke. This is really basic, basic functionality of C++, to the extent
that I can't imagine anyone would be using a compiler these days that
didn't produce the same output. I just decided to show a program to answer
the question instead of answering it in words; I was in a coding kind of
mood (and besides, I had just answered the OP's other adjacent question in
words, so this was kind of like a supplement to that answer...)
-leor
 
L

Leor Zolman

In composing the code for my sample program in the "Is init done for arrays
in classes" thread, here is an earlier version I had:

#include <iostream>
using namespace std;

class B {
//...
public:
B() { std::cout << "B() "; }
};

class A {
B b[100];
public:
A() { std::cout << "A()"; }
//...
};

int main()
{
A a();
return 0;
}


It compiled with no warnings or errors under Comeau (in my default
configuration, which isn't 'strict' mode), and when run, produced no output
whatsoever. Without comparing it to the actual code I posted, or trying to
compile it with another platform, can you find the bug? Please do not
bother to post your answer, it'll just clutter the NG. This is a "silent"
exercise, and reflects one of my pet C++ "embarrassments" ;-)
-leor
 
V

Victor Bazarov

Leor said:

It's actually in the FAQ. Posting such "quiz" is supposed to be pointless
since every reader here should begin with the FAQ and should know the
answer to your "quiz". Of course, not many are attentive enough to spot
the "but", but once it's encountered, it's most likely to be remembered...
 
L

Leor Zolman

Leor said:

It's actually in the FAQ. Posting such "quiz" is supposed to be pointless
since every reader here should begin with the FAQ and should know the
answer to your "quiz". Of course, not many are attentive enough to spot
the "but", but once it's encountered, it's most likely to be remembered...

Yeah, except for the last part. I just spent five minutes today wracking my
brain over why I wasn't getting any output. I'd simply transcribed the OPs
syntax into my local definition. This may serve to do nothing more than
advertise the fact I can be a space case, and if so, my apologies to anyone
who considers it a waste of bandwidth and I won't do it again.
-leor
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Leor said:
In composing the code for my sample program in the "Is init done for
arrays in classes" thread, here is an earlier version I had:

#include <iostream>
using namespace std;

class B {
//...
public:
B() { std::cout << "B() "; }
};

class A {
B b[100];
public:
A() { std::cout << "A()"; }
//...
};

int main()
{
A a();

This is the declaration of a function called a the returns an A.
 
V

velthuijsen

It compiled with no warnings or errors under Comeau (in my default
configuration, which isn't 'strict' mode), and when run, produced no output
whatsoever. Without comparing it to the actual code I posted, or trying to
compile it with another platform, can you find the bug? Please do not
bother to post your answer, it'll just clutter the NG. This is a "silent"
exercise, and reflects one of my pet C++ "embarrassments" ;-)

Wierd, my compiler chucks out
'A a(void)': prototyped function not called (was a variable definition intended?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top