0

Could someone please explain what is meant by the following?

You must define a default constructor if your class defines member variables and has no other constructors. Otherwise the compiler will do it for you, badly.

What are they referring to as "badly"?

user541686
  • 205,094
  • 128
  • 528
  • 886
  • I don't know, but it is wrong. Where did you get it from? Oh, the infamous Google C++ style guides. Just about everything in them is wrong, but this is a classic. There is a huge thread on the style guides on comp.lang.c++.moderated, if you are interested. –  May 24 '11 at 16:37
  • The Google style guide (see the link). Oh wait, what? I actually agree with lots of them, but some of them are making my jaw drop... – user541686 May 24 '11 at 16:37
  • I don't know about "badly" but consider the quandary for the poor schlub who has to maintain the code. If the member variables are defaulted did the original coder intend to use defaults or did he (indefinite pronoun sense) simply not know any better? – Onorio Catenacci May 24 '11 at 16:43
  • I don't buy that style guide entry for a second. *It* is bad, IMO. Aggregate initialization is superior to a manual constructor. DRY; a constructor doesn't add anything in such a case. According to them, `std::array<>` would need to be redesigned? – Johannes Schaub - litb May 24 '11 at 18:23

11 Answers11

5

From the expansion of that link:

"The reason for this is that if you have no other constructors and do not define a default constructor, the compiler will generate one for you. This compiler generated constructor may not initialize your object sensibly."

Matt K
  • 13,370
  • 2
  • 32
  • 51
  • 1
    @mkb: LOL, I *did* expand the link, but I just noticed the `definition`, not the pros/cons. Thanks – user541686 May 24 '11 at 16:40
  • 1
    +1: indeed. So the rule should really be "*if you care* about having sensible default initial values, you should write your own default constructor". But that's common sense, really. – Oliver Charlesworth May 24 '11 at 16:40
  • @Oli No, I don't think it is. I rarely provide a default constructor for my classes. –  May 24 '11 at 16:42
  • 1
    @Mehrad: hahaha.. even I saw only the *definition*. Google's UX is very bad :P... they should use some color-coding to draw the border of the expander more clearly. – Nawaz May 24 '11 at 16:45
  • @Neil: Indeed. But I bet the cases where you do provide one is where you *do* care about having sensible defaults? – Oliver Charlesworth May 24 '11 at 16:46
  • Well, the expander looks like a play button, too, so I don't blame you. I was expecting the button to play a recording of someone reading the item. – Matt K May 24 '11 at 17:54
5

Might refer to how new T and new T() differ when there is no ctor provided.

Community
  • 1
  • 1
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
1

It's good to be sure that the object is created in a known state. Primitive variables won't be set to zero by default, so you could end up with subtle bugs that don't always show up. By initializing the member variables to sensible variables, everything is much more predictable.

James Johnston
  • 9,264
  • 9
  • 48
  • 76
1

The only problem with the default constructor is that it initializes only what the compiler thinks must be initialized, and not what you may think needs to be initialized. Basically, that means that it will invoke initializers for objects with default initializers. It won't set pointers or simple types like int to sane values, etc. If that is sufficient, then the default constructor is not 'bad'. When it is insufficient, it is a bug (in your code) that you did not define the necessary default constructor with the correct initialization.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

Take the Google style guide with a grain of salt -- or maybe a truckload of salt.

It is true that the compiler-generated default constructor won't necessarily initialize members that are of built-in types in a meaningful fashion. If you want that done, then yes, its failure to do that is bad. OTOH, if you don't want that done, then its doing it could be somewhat bad (wasteful) as well.

Bottom line: there are times to write your own default ctor, but they tend toward the exception, not the rule. Although there are simple rules of thumb to cover a lot of cases in C++ and will prevent a lot of problems, this really isn't one of them -- here you pretty much do need to know what the compiler-generated ctor will do, and what you want different if you're going to write your own.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

In Debug build most compilers fill uninitialized space with some magic values, so that debugging is reliable. And providing custom constructor prevents certain POD optimizations.

John
  • 2,295
  • 1
  • 20
  • 25
0

In fact, it's a guideline just to make sure people does explicitely make the statement of what is an invalid or default state of any object.

That way, no surprise when reading the code, compared to the actual execution.

However, think that it's a company guideline, that is used to make sure everyone does follow the same rules, that's not a you-must-follow-it-because-google-does-it.

In fact, if you manage to make all your member objects being in valid state when default constructed, or force you to set a constructor, then there is no good reason for such a guideline.

Klaim
  • 67,274
  • 36
  • 133
  • 188
0

If you have any primitive types as member variables (eg. int, float), then the default ctor will not initialize them. member variables that are a class type will have their default ctor's invoked.

Prefer member initializer lists, so your user supplied ctor may be empty:

class Foo {
 int bar;
 float baz;

 Foo(): bar(0), baz(0.0f) { /* empty ctor body */ }

};
dchev
  • 1
0

It won't set integers to 0 or pointers to null. It will run default constructors on object of types with constructors.

Some people would call it 'not sensible'.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
0

It just seems a too simplified version of the rules of 3, you should either define yourself or leave the compiler version of

  • the copy constructor
  • the assignment operator
  • the destructor

(Note that by defining yourself a copy constructor, the compiler won't define a default constructor).

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
-1

The default constructor built by the compiler does 'nothing', it will not even zero the memory occupied by the object

Andrew
  • 2,530
  • 16
  • 9
  • Um, how wrong can you be. It doesn't zero initialise (and why should it? what is this mania with zero initialisation?) , but it also does not do "nothing". –  May 24 '11 at 16:43
  • Golly I dunno, not as smart as you apparently. Although I have seen my fair share of expensive systems brought to their knees by somebody not initialzing something completely. It was some other author referring to the default ctor built by the compiler as bad, and the intitial question was 'why would that be' and I as offering up just one of many possible perspectives as once again 'fodder' for the nit picking little ---- who thrill in laying other's low and rejoicing in the misery the inflict whilst at the same time contributing little. – Andrew May 24 '11 at 17:05
  • I'm sorry you think I contribute little (if that is your meaning). Looked at objectively, I seem to have contributed in the past month around 6 times more than you did in the past year. –  May 24 '11 at 17:10
  • ahhh, so we get to the truth of it, the quest for one-upsmenship. Cool, in that case, I conceed and you win, your contributions shall now and forever supercede mine as I am withdrawing mine. Contgrats, one more down. – Andrew May 24 '11 at 17:27
  • You accuse me of not contributing, and then when I defend myself against that accusation accuse me of "one-upmenship"? Cool. –  May 24 '11 at 17:35