Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

will global variable for class type be initialized at declaration?

1 view
Skip to first unread message

Chris

unread,
Nov 20, 2009, 2:10:07 PM11/20/09
to
hi, if i have a local variable as
A a;

in a class. I declare it in header and define/intialize it in the cpp file,
normally.

If I declare it as a global variable in my project, will that be initialized
at the point of declaraion?
thanks

Scott McPhillips [MVP]

unread,
Nov 20, 2009, 2:20:24 PM11/20/09
to
"Chris" <Ch...@discussions.microsoft.com> wrote in message
news:CF9AE349-20A1-4126...@microsoft.com...

Yes, globals are initialized before main or WinMain are called.


--
Scott McPhillips [VC++ MVP]

David Wilkinson

unread,
Nov 20, 2009, 2:42:56 PM11/20/09
to

Chris:

What do you mean by "local variable in a class"?

Non-static (instance) member variable?

or

Static member variable?

Instance member variables are initialized with the object instance.

Static member variables are much like globals. As Scott says, they are
initialized before main() is entered.

--
David Wilkinson
Visual C++ MVP

chrisben

unread,
Nov 20, 2009, 3:16:01 PM11/20/09
to
thanks.

another question:

class B;
class A
{
public:
vector<B> v;
B b;
A(){}
};

will v and b be initialized in the above class? If not, how should I?
Thanks

Chris

"Scott McPhillips [MVP]" wrote:

> .
>

chrisben

unread,
Nov 20, 2009, 4:06:05 PM11/20/09
to
non-static, i refer to B b; as my last example. thanks

"David Wilkinson" wrote:

> .
>

David Wilkinson

unread,
Nov 20, 2009, 4:19:45 PM11/20/09
to
chrisben wrote:
> thanks.
>
> another question:
>
> class B;
> class A
> {
> public:
> vector<B> v;
> B b;
> A(){}
> };
>
> will v and b be initialized in the above class? If not, how should I?

Your code will not compile as given because class B is not defined (and for
other reasons).

But if the code were complete, the answer is that b and v will be initialized
for each A object that you create, but not otherwise.

chrisben

unread,
Nov 21, 2009, 12:51:02 PM11/21/09
to

I am a little confused. Assuming my class is like

class A
{
public:

int i;



vector<B> v;
B b;
A(){}
};


If I do not initialize i, as
int A::i = 0;

I 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?

"David Wilkinson" wrote:

> .
>

Scott McPhillips [MVP]

unread,
Nov 21, 2009, 1:09:27 PM11/21/09
to
"chrisben" <chri...@discussions.microsoft.com> wrote in message
news:9DB35576-0421-4B65...@microsoft.com...

>
> I am a little confused. Assuming my class is like
>
> class A
> {
> public:
>
> int i;
>
> vector<B> v;
> B b;
> A(){}
> };
>
>
> If I do not initialize i, as
> int A::i = 0;
>
> I 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?


Classes have constructors, which are called when they are instantiated. For
example, vector is initialized to an empty state by its constructor when it
is created.

But integers and other "plain old data" types are not classes and do not
have constructors in C++. So yes, there could be anything in i.

Igor Tandetnik

unread,
Nov 21, 2009, 1:14:18 PM11/21/09
to
chrisben wrote:
> I am a little confused. Assuming my class is like
>
> class A
> {
> public:
>
> 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) {}

> I 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

chrisben

unread,
Nov 21, 2009, 2:07:01 PM11/21/09
to
thank you guys for your patience.
It is not my book's fault, which I have left it for a while after working on
java/c#. I slap my face after reading it :).
have a good weekend

"Igor Tandetnik" wrote:

> .
>

0 new messages