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()?
Asked
Active
Viewed 415 times
1
-
2Add a constructor to the class/struct? – Some programmer dude Jan 08 '13 at 12:27
-
2Constructor initializer list? – hmjd Jan 08 '13 at 12:27
-
4`class Foo { int i; Foo() : i(0) {} };` – jrok Jan 08 '13 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 08 '13 at 12:29
-
Thanks Joachim, hmjd, jrok and chris. I'll add a constructor. – NeonGlow Jan 08 '13 at 12:29
-
what about this? class clasName{ int x1= 0; int x2= 0; int x3= 0; int x4= 0; int x5= 0; } – Dipak Jan 08 '13 at 12:31
-
@Dipak, Works great, but only in C++11. – chris Jan 08 '13 at 12:31
-
I don't think any compiler supports that syntax yet. – jrok Jan 08 '13 at 12:32
-
@chris : Thanks. I want this object to be allocated from heap. – NeonGlow Jan 08 '13 at 12:32
-
@jrok, I know GCC does. I don't think the other two do. MSVC definitely doesn't. – chris Jan 08 '13 at 12:32
-
@NeonGlow, Then I suggest a smart pointer. – chris Jan 08 '13 at 12:33
-
@chris Cool, then it's about time I update. – jrok Jan 08 '13 at 12:35
-
@jrok, Ah, Clang does as well, since 3.0. I saw that talk about Clang that was posted recently, but I guess I got mixed up and thought it wan't. – chris Jan 08 '13 at 12:36
-
I am working with GCC and target is MIPS32. So hopefully dipaks method will work. I'll check and update. – NeonGlow Jan 08 '13 at 12:40
-
Dipaks method is giving me an error "ISO C++ forbids initializtion of member". – NeonGlow Jan 08 '13 at 12:55
-
Try with `-std=c++0x` option. – jrok Jan 08 '13 at 13:01
3 Answers
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
-
2Not 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
-
-
@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.
-
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
-
1Now 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