Post by chrisbenI am a little confused. Assuming my class is like
class A
{
int i;
vector<B> v;
B b;
A(){}
};
If I do not initialize i, as
int A::i = 0;
This can only be done for static members (try it - you'll get a compiler error). Non-static members should be initialized in the constructor, as in
A::A() : i(0) {}
Post by chrisbenI think i is not initialized and there could be anyting in i, when I do "A
a;".
So why can a class type as v and B be initialized?
Because B and vector are classes and have their own constructors. Default constructors for b and v are automatically invoked as part of A's constructor. On the other hand, plain ints don't have constructors, and so need to be initialized explicitly.
If your C++ textbook doesn't explain this, I strongly suggest you get a better one. This is really basic stuff.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925