开发者

When do we need to have a default constructor?

开发者 https://www.devze.com 2023-02-20 19:38 出处:网络
My question is simple. When do we need to have a default constructor? Please refer to the code below:

My question is simple. When do we need to have a default constructor? Please refer to the code below:

class Shape
{
    int k;

public:
    Shape(int n) : k(n) {}
    ~Shape() {}
};

class Rect : public Shape
{
    int l;

public:
    Rect(int n): l(n)
    {}      //error C2512: 'Shape' : no appropriate default constructor available

    ~Rect() {}
};
  1. Why is the compiler not generating the zero argument default constructor implicitly in the class Rect?

  2. As per my knowledge, if a class (Rect) is derived from another class (Shape) that has default constructor (either implicitly 开发者_运维百科generated or explicitly provided), the default constructor should be generated by the compiler.


A default constructor is not synthesised if you created your own constructor with arguments. Since you gave Shape a constructor of your own, you'd have to explicitly write out a default Shape constructor now:

class Shape
{
      int k;

  public:
      Shape() : k(0) {}
      Shape(int n) : k(n) {}
      ~Shape() {}
};

(You can leave out the empty ~Rect() {} definitions, as these will be synthesised.)

However, it looks to me like you don't want a default constructor for Shape here. Have Rect construct the Shape base properly:

class Shape
{
      int area; // I've had to guess at what this member means. What is "k"?!

  public:
      Shape(const int area)
         : area(area)
      {}
};

class Rect : public Shape
{
     int l;
     int w;

  public:
     Rect(const int l, const int w)
        : Shape(l*w)
        , l(l)
        , w(w)
     {}
};

Also note that this example is oft cited as an abuse of OO. Consider whether you really need inheritance here.


A default constructor will only be automatically generated by the compiler if no other constructors are defined. Regardless of any inheritance.

And also you need to construct your base class by calling:

Rect( int n ) : Shape( n ), l(n)
{
}


The compiler will define a default ctor if and only if you don't explicitly declare any ctors.

Note that what's important is declaring the constructor, not necessarily defining it. It's fairly common, for example, to declare a private ctor, and never define it, to prevent the compiler from implicitly defining any others.

Edit: Also note that C++11 has an =default syntax for dealing with situations like yours.


See this for the full behaviors of C++ WRT constructors: http://en.wikipedia.org/wiki/Default_constructor

The simple answer is that if you specify a constructor, the compiler will not create a default one for you.

This rule applies to Java as well.


Compiler generates default constructor in case when you have not define any constructor. But if you have defined any constructor that takes some argument or not. The compiler will use that constructor and will not generate default constructor with zero argument.


The default constructor is generated only if you have not defined any other constructors.

Supposedly, if you need some special initialization in the class, the default constructor would not do the right thing.


As you defined a Constructor for Shape expecting an integer you have overwritten the default constructor by doing so. So if you extend Shape you must pass an integer value to the superclass.

0

精彩评论

暂无评论...
验证码 换一张
取 消