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"?

4

11 回答 11

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."

于 2011-05-24T16:39:12.407 回答
5

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

于 2011-05-24T16:40:59.160 回答
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.

于 2011-05-24T16:40:06.467 回答
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.

于 2011-05-24T16:41:44.447 回答
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.

于 2011-05-24T16:43:20.090 回答
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.

于 2011-05-24T16:42:56.713 回答
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.

于 2011-05-24T16:43:25.860 回答
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 */ }

};
于 2011-05-24T16:43:56.650 回答
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'.

于 2011-05-24T16:45:05.450 回答
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).

于 2011-05-24T16:49:46.260 回答
-1

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

于 2011-05-24T16:42:26.283 回答