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()?

16
  • 2
    Add a constructor to the class/struct? Jan 8, 2013 at 12:27
  • 2
    Constructor initializer list?
    – hmjd
    Jan 8, 2013 at 12:27
  • 4
    class Foo { int i; Foo() : i(0) {} };
    – jrok
    Jan 8, 2013 at 12:28
  • I just want to make it clear that you don't have to use new to create a new object. ClassName objectName; works better in most cases.
    – chris
    Jan 8, 2013 at 12:29
  • Thanks Joachim, hmjd, jrok and chris. I'll add a constructor.
    – NeonGlow
    Jan 8, 2013 at 12:29

3 Answers 3

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.

8
  • 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 8, 2013 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 8, 2013 at 12:41
  • I got a warning [GCC], when I tried to initialize in reverse order.
    – NeonGlow
    Jan 8, 2013 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 8, 2013 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 8, 2013 at 13:51
3

Value initialization:

struct X
{
    int i, j;
};

X* x = new X(); // The '()' are required.
// 0 == x->i && 0 == x->j
3
  • Are suggesting to update each member individually? My struct has a lot of members which makes it difficult. :(
    – NeonGlow
    Jan 8, 2013 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 is a good read.
    – chris
    Jan 8, 2013 at 12:43
  • 1
    @NeonGlow, no. The comment in the code snippet is expressing the result of the value initialization.
    – hmjd
    Jan 8, 2013 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.

6
  • 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 8, 2013 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 8, 2013 at 13:03
  • @jrok : edit it or post your comment as answer. and close this question. :)
    – Dipak
    Jan 8, 2013 at 13:07
  • @NeonGlow : "ISO C++ forbids initialization of member" is just against convention not error?
    – Dipak
    Jan 8, 2013 at 13:10
  • @NeonGlow : what about assigning value to class variable from it constructor?
    – Dipak
    Jan 9, 2013 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.