[C ++ learning] FAQ about default constructor

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

1. What is the default constructor?

A function is the default constructor.When and only whenYou can call it without passing in any parameters. This function can be customized or generated by the compiler. The constructors below are Default constructors.

struct A
{
   int x;
   A(): x(0) {}
};
struct B: A
{
//no user-defined ctor.
//the compiler implicitly declares a default constructor
};
class C
{
public:
   explicit C(int n=0, int m=0); // may be invoked with no arguments
};

2. When does the compiler implicitly declare the default constructor?

There are two conditions:

  • This class is not explicitly declaredAnyConstructor. -- Since you have defined it, the system will not generate it for you.
  • No const or reference exists in the data member. -- Because Initialization is required.

If yes, the default constructor is generated implicitly. Here we emphasize"AnyIt means that the default constructor will not be implicitly declared even if you have customized a replication constructor or a constructor that requires multiple parameters.

Therefore, when defining a class, make sure that there are Default constructors.

3. What does the default constructor do with implicit declaration?

Do nothing!Specifically, whether it is an implicit or explicit declaration, it does not mean that the constructor is defined. Let's take a look at the following situation:

class B
{
 B(); //declaration only
};
B::B() {} //separate definition of the user-declared default constructor
class C
{
C() {} //declaration and definition
};
class D
{
//implicit declaration of a default constructor, no implicit definition
};
class E
{
//implicit declaration + implicit definition of a default constructor
virtual void f();
};
class F
{
virtual void f();
public:
F(); ////explicit declaration a default constructor, user forgot a definition
};

The last case is a common error. If you forget to define the explicit default constructor, this will lead to a link error-the User-Defined default constructor must be implemented, even if it is empty implement.

We have noticed that Class D is implicitly declared, but there is no implicit definition. Since there is no implicit definition, why should the compiler implicitly declare it?

First, we need to further clarify the essence of the implicit declaration and implicit definition. Both actions are conceptual. The Compiler does not actually insert the relevant code into your header file, it only shows the behavior of compilers, connectors, and programs.It seems that this action was performed. In fact, the compiler only sets several bits for identification. The compiler only specifies how a class can be used (each implicit declaration is like an article in a contract that states how a certain class may be used. when the compiler implicitly declares one of the special member functions, it grants certain authorizations to the user .)

4. When will the compiler implicitly define an implicit declared default constructor?

Any of the following conditions is implicitly defined:

  • Class with virtual member functions-Initialize vptr.
  • Subclass-implicitly executes the default constructor of the base class.
#include 
     
using namespace std;
class Bar
{
public:
  Bar(){cout<< "Default constructor!"<
     
      < pre>
     
};
class Too:public Bar
{
};
//Foo::Foo(Bar bar):bar(bar){}
int main()
{
    Too too1;
    return 0;
}


  • Virtual base class inheritance.

Note: The implicitly defined constructor itself does not allocate memory. It is only the initialization of A Class Object. After the memory is allocated when the object is declared, the constructor is executed. Data members are not initialized.

Author: gnuhpc

Source: http://www.cnblogs.com/gnuhpc/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.