Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Foo {
- private:
- int myInt;
- };
- int Foo::myInt = 1;
- class Foo {
- private:
- int myInt;
- public:
- Foo() : myInt(1) {}
- };
- class Foo {
- private:
- static const int myInt = 1;
- };
- class Foo {
- private:
- static int myInt;
- };
- class Foo
- {
- public:
- Foo(void) :
- myInt(1) // directly construct myInt with 1.
- {
- }
- // works but not preferred:
- /*
- Foo(void)
- {
- myInt = 1; // not preferred because myInt is default constructed then assigned
- // but with POD types this makes little difference. for consistency
- // however, it's best to put it in the initializer list, as above
- // Edit, from comment: Also, for const variables and references,
- // they must be directly constructed with a valid value, so they
- // must be put in the initializer list.
- }
- */
- private:
- int myInt;
- };
Add Comment
Please, Sign In to add comment