Does a constructor / destructor have to have code, or is the function enough?

前端 未结 3 1247
再見小時候
再見小時候 2021-01-24 17:44

So if I have a class

class Transaction {

int no;
char dollar;

public:

    Transaction();
    ~Transaction();
}

And in my constructor / destr

相关标签:
3条回答
  • 2021-01-24 18:07

    Your class has only simple data members, so you don't even need (or should want) a destructor.

    You should, however, initialize your data members in the constructor:

    Transaction::Transaction()
      : no(0), dollar('$') {
        cout << "Entering constructor" << endl;
    }
    

    Otherwise, they will not be initialized, and may contain random values (C++ doesn't guarantee initialization of primitive non-static members to any particular value if you don't initialize them in the constructor). This can be particularly insidious if you have pointer members.

    0 讨论(0)
  • 2021-01-24 18:14

    No, it does not have to have code. In fact, in most languages you can leave it out and it will construct the class properly.

    0 讨论(0)
  • 2021-01-24 18:34

    Each subobject will be default initialized, if you provide a constructor without specifying initializers for said subobject.

    Section 8.5 of the Standard provides that:

    If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [ Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. — end note ]

    and that

    To default-initialize an object of type T means:

    • if T is a (possibly cv-qualified) class type, the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    • if T is an array type, each element is default-initialized;
    • otherwise, no initialization is performed.

    If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

    There are two ways to initialize subobjects: in the ctor-initializer-list, and in a brace-or-equal-initializer in the member declaration (for non-static members the latter is new in C++11).

    In practice, this means that when you don't provide an initializer, variables of primitive type, such as int and char, retain whatever value was left in memory previously. In most cases it's extremely hard to predict what value that will be, but you should be aware that it could be a leftover sensitive datum such as a password.

    In the case of initialization of variables of static storage duration (such as objects at namespace scope and their members), the Standard further provides (same section) that:

    Every object of static storage duration is zero-initialized at program startup before any other initialization takes place.

    This is subtly different from what happens during value-initialization if you did not define a constructor:

    To value-initialize an object of type T means:

    • if T is a (possibly cv-qualified) class type with a user-provided constructor, then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    • if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.
    • if T is an array type, then each element is value-initialized;
    • otherwise, the object is zero-initialized.

    but the effect is the same -- primitive members are set to zero and all others have their zero-argument constructor invoked.

    nneonneo's advice to initialize all members explicitly is good, since having zero-initialization only for variables of static storage duration often leads to difficult-to-find bugs. But it's perfectly viable to use the brace-or-equal-initializer technique:

    class Transaction
    {
        int no = 0;
        char dollar = 0;
    public:
    
        Transaction();
        ~Transaction();
    }
    
    0 讨论(0)
提交回复
热议问题