1

I have a class in my code that has some integer data members but no methods. Is there exist a way to initialize all the data items with 0 when I create a new object of it using new()?

NeonGlow
  • 1,659
  • 4
  • 18
  • 36

3 Answers3

5
class A
{
    int x_; 
    int y_; 

    public: 
        A() 
           : 
             x_(0),
             y_(0)
        {} 
};

The part of the A() constructor before the brackets "{}" is called an "initializer list", which you use to initialize the variables to a default value 0 in the case above, or to some other values that may be passed to a constructor that might take those values as arguments. However you initialize an object of type A (e.g. with "new"), the attributes will be initialized to those values. It is good coding style to initialize the attributes in the same order they are declared, it makes the code more readable.

tmaric
  • 5,347
  • 4
  • 42
  • 75
  • 2
    Not only that, they get initialized in the order they're declared in the class. `: y_(0), x_(y)` is a bad thing to do, since `x` won't be initialized properly. – chris Jan 08 '13 at 12:38
  • @chris: I know, but on some compilers without compiler options that output all warnings set on, not even a warning is produced during compilation, so it's good to point out that the initialization order should follow the order in which the attributes are declared.. – tmaric Jan 08 '13 at 12:41
  • I got a warning [GCC], when I tried to initialize in reverse order. – NeonGlow Jan 08 '13 at 13:39
  • @NeonGlow I just re-run g++, without compiler flags, and with the compiler "g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3" and compiling the code with reversed intialization doesn't give me any warnings... – tmaric Jan 08 '13 at 13:41
  • @tomislav-maric : It was not an error. Just a warning. Says "x and y will be re-ordered to match declaration order" – NeonGlow Jan 08 '13 at 13:51
  • @NeonGlow, I didn't write error, I'm also talking about warnings... :) What compiler are you using, did you set any compiler flags? Like, if I use "g++ -Wall", then I get that warning as well, but if I only use g++, I don't get it. – tmaric Jan 08 '13 at 13:58
  • @tomislav-maric : Mine is "(GCC) 4.3.2 20081105 (Red Hat 4.3.2-7)". i think the strict checking is enabled. – NeonGlow Jan 08 '13 at 14:21
  • @NeonGlow, this is a learning example and yet different compilers show different output.. it's interesting :) – tmaric Jan 08 '13 at 14:27
3

Value initialization:

struct X
{
    int i, j;
};

X* x = new X(); // The '()' are required.
// 0 == x->i && 0 == x->j
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Are suggesting to update each member individually? My struct has a lot of members which makes it difficult. :( – NeonGlow Jan 08 '13 at 12:42
  • @NeonGlow, It will value-initialize everything, which means `int` etc. will be initialized as well, not just the non-trivially-constructible types that would in default-initialization. If you want to see what happens in detail, [this one](http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new) is a good read. – chris Jan 08 '13 at 12:43
  • 1
    @NeonGlow, no. The comment in the code snippet is expressing the result of the value initialization. – hmjd Jan 08 '13 at 12:44
2
class clasName{
 int x1= 0;
 int x2= 0;
 int x3= 0;
 int x4= 0;
 int x5= 0;
}

Only in C++11.

jrok
  • 54,456
  • 9
  • 109
  • 141
Dipak
  • 6,532
  • 8
  • 63
  • 87
  • 1
    @Dipak : Flagging an error "ISO C++ forbids initialization of member" : gcc (GCC) 4.3.2 20081105 (Red Hat 4.3.2-7) – NeonGlow Jan 08 '13 at 12:49
  • 1
    Now you messed up the answer completely - you're doing assignment of member. You better roll back the edit and read up some more about the topic. – jrok Jan 08 '13 at 13:03
  • @jrok : edit it or post your comment as answer. and close this question. :) – Dipak Jan 08 '13 at 13:07
  • @NeonGlow : "ISO C++ forbids initialization of member" is just against convention not error? – Dipak Jan 08 '13 at 13:10
  • @NeonGlow : what about assigning value to class variable from it constructor? – Dipak Jan 09 '13 at 04:09
  • @Dipak : Yes it will be fine. I think the initializer list option is equally good. – NeonGlow Jan 09 '13 at 04:13