m reading c++ primer 4th edition by Stanley Lipmann and I'm on page 92 about value initialisation.

I just don't understand when about value initialisation. I browsed around and I know there are also default initialisation and zero-initialisation. Can anyone explain about value initialisation?

Coming to this paragraph ..

clause a)

"Some classes does not define default constructor. We cannot initialise vector of such a type by specifying only a size, we must also specify an initial value"

I do understand the above but I find that the below contradict the above sentence.

clause b)

"Element type might be of a class type that does not define any constructors. In this case, the library still creates a value-initialised object. It does so by value-initialising each member of that object"

I don't understand the clause b.

Any help is appreciated

Sorry. I don't get it. What's wrong with posting here? Are you all related? If not please explain what's wrong to seek answer from other people in this world? There are times when I don't feel I'm getting satisfactory answer from one website, i might get an acceptable answer to me on other side. Please explain

Nothing wrong with asking one question here and one question there, but it's bad netiquette to ask the exact same question at the exact same time on two forums without alerting each forum about the other. It causes people on one forum to craft a response that will never be read because it's already been answered on the other one. Had they known, they would not have answered. It wastes time and effort and makes everyone mad.

Read this...


Don't shotgun-blast all the available help channels at once, that's like yelling and irritates people. Step through them softly.

I do understand but I assume that each website are totally different from others and people are also different too. Think about it. If all these website are answered by the same group of people .. why do i waste my time posting here in the first place? So please confirm if you all - the same people are answering threads on javaranch, daniweb and stackoverflow? Any other website you guys are answering threads?

All the websites are different with different people answering. That said, there are many people who answer questions on more than one forum, so there's overlap. What all the forums have in common is that everyone hates it when people ask the same question on more than one forum, because, as mentioned, when that happens, you're very often answering a question that's already been answered without your knowledge. It's the same reason that you are asked to mark a thread "Solved" here. Why would I want to answer a question that's already been answered? I can't explain it better than that.

Anyway, it's considered rude, take my word for it, or start a thread in the Daniweb Community Feedback forum and see what others think. If you weren't aware of that before, no worries, but now you are.

For the answer to your question, please refer to section 8.5 and 12.1/5-6-7-8 of the C++ standard (1998).

And, yes, it is irritating when posting on several forums. Different forums have different feels to them, and some do indeed participate in many.

How many different answers are you expecting? If you get three different answers, which one do you choose? Doesn't having multiple answers give you more problems?

And if you only get one answer from three different sites, who just wasted the time of people on two sites? Why do you consider that a good thing?

Ok. I understand now. Problem is there are many times, I couldn't get a satisfactory answer that I can understand .. Maybe it's just me. By the way, just curious .. you guys getting paid doing this? :OP

Ok. I understand now. Problem is there are many times, I couldn't get a satisfactory answer that I can understand .. Maybe it's just me.

Then explain why you don't understand and ask for clarification. Key is explain -- don't just say you don't understand.

By the way, just curious .. you guys getting paid doing this? :OP


>>By the way, just curious .. you guys getting paid doing this? :OP

No. This is purely voluntary. There are different reasons for spending some time here and there to answer a few questions (e.g. helping people, promoting good values, strengthening your own understanding, having interesting discussions and sharing knowledge, ego-tripping!, etc.), but none involve money. So, voluntary means we can choose to answer, but we can choose to ignore. Questions which are short, unexplicative, or sound like "I don't get this! please explain!", can get ignored fairly often (and voted down), just like they would in real life (i.e. if your friend came up to you and said "I can't understand this homework! do it for me!", you wouldn't respond and probably told him to do his own bloody homework, but if he comes up and says "I've been working all day on this problem! This is how far I got.. but I'm stuck, what am I missing?", you would be much more inclined to answer).

Here's the answer to the original question.

If you have a class that defines one or more constructors explicitly, but does not define a default constructor, then every object of that class must be explicitly initialized. For example:

struct A {
    int n;
    A(int i): n(i) { }

Here, I have defined a class with a constructor (A(int)) but no default constructor. Therefore, if I write

A a;

my program will not compile because I did not initialize my variable.

Such a type cannot be used as an element type for a vector:

vector<A> v(10);

because there is no way to initialize v's elements.

OK, that handles case (a).

Case (b) talks about a class that does not define any constructors at all. In that case, the class gets a default constructor automatically, and that default constructor value-initializes each data member of the class. For example:

struct B {
    int b;

Now the expression B() calls the (implicitly generated) default constructor of B, which value-initializes the data member. So, for example:

void f()
    B b1;        // b1.b is uninitialized
    B b2 = B();  // b2.b is zero because B() is value-initialized

In summary:

When a class defines one or more constructors but does not define a default constructor, it does not get a default constructor; every time you create an object of that class, you must initialize every object of that class.

When a class does not define any constructors at all, not even a default constructor, it gets a default constructor that value-initializes every data member.

Thanks for your explanation. So on that f() function, it seems b1 object has a member called b which is uninitialized. Meaning in C++, it allows creation of an object with the possibility of some member in it being uninitialized? Did i get it right? In java, this is forbidden. All object member will be initialized to its default value if nothing is given. Since B doesnt have any constructor (hence a default constructor is given), in order to initialize b2 and all its member, one has to do the =B();? (else it'll be just like b1)

If you wanted to be sure that member b was always initialized, you should have written a default constructor to initialize it. The reason for this rule is the possibility that a class might contain a (potentially) large array that you might not want to initialize all at once.

WOW .. i just realized you are Andrew Koenig! I'm so honored you answered my question. I bought one of your book but have not gone into it. Thanks.