|
-
November 16th, 2010, 10:48 AM
#2
Re: Why can't member variable be initialized?
If I understand your question correctly, you're asking why you can't do this:
Code:
struct A
{
int i = 0;
};
?
If that's the question, then the answer is: because it isn't clear when that assignment should take place written in that way. Any code outside of a function (global initializations etc) must be executed before main(), but here there is not yet any object to initialize. So when would the assignment take place? Presumably, upon construction of an A object, this would specify the initial values of its fields.
That *would* be a perfectly reasonable interpretation....but that just isn't the way they chose to specify it. Instead, you have to do
Code:
struct A
{
int i;
A(): i(0) {}
};
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|