简体   繁体   English

用C ++调用构造函数

[英]Constructor calling in c++

Memory is allocated to variables inside a class only after the objects are created ..right? 只有在创建对象之后,才将内存分配给类中的变量。 So what if the class contain a variable which is an object of another class and the variable has a constructor? 那么,如果该类包含一个变量,该变量是另一个类的对象,并且该变量具有构造函数,该怎么办?

class Wand
{
    mouse mouseEmu(0,0);
    QCursor pt;
};

mouseEmu and QCursor are two classes... mouseEmuQCursor是两个类。

When will be the constructor of mouseEmu called and when will be the default constructor for pt called? 什么mouseEmu调用mouseEmu的构造函数, mouseEmu调用pt的默认构造函数?

Is it necessary to call the parametrised constructor if we specify one? 如果指定一个,是否有必要调用参数化构造函数?

when will be the constructor of mouseEmu called and when will be the default constructor for pt called? 什么时候调用mouseEmu的构造函数,什么时候调用pt的默认构造函数?

All data member and base-class sub-objects of the currently constructed object all fully constructed before the body of the constructor of the current object is entered. 在输入当前对象的构造函数的主体之前,当前构造的对象的所有数据成员和基类子对象都已完全构造。 So if your class had a user-defined constructor, then by the time the code inside the user-defined constructor is executed, all the other sub-objects of the class have had their constructors completed. 因此,如果您的类具有用户定义的构造函数,则在执行用户定义的构造函数中的代码时,该类的所有其他子对象都已完成其构造函数。

If for some reason you need to setup a user-defined initialized value for a sub-object, and that sub-object has a non-default constructor, then you can use an initialization list with the constructor of the current object to pass values to the constructor of the sub-object. 如果由于某种原因需要为子对象设置用户定义的初始化值,并且该子对象具有非默认构造函数,则可以将初始化列表与当前对象的构造函数一起使用,以将值传递给子对象的构造函数。 For instance: 例如:

class Wand
{
    mouse mouseEmu;
    QCursor pt;

    Wand(): mouseEmu(0,0) {} //empty constructor body with initialization list
};

New Question (or old depending on when you arrived) 新问题(旧问题取决于您到达的时间)

class Wand
{
    mouse mouseEmu(0,0);
             //   ^^^^^^  This bit is illegal.
             //           Remove it here. You specify the parameters
             //           Passed to members in the constructor.
    QCursor pt;
};

What you want is: 您想要的是:

class Wand
{
    Ward(): mouseEmu(0,0) {}
    mouse mouseEmu;
    QCursor pt;
};

Edit: 编辑:

Answer based on the question before it was changed to illegal. 根据问题的答案,然后再将其更改为非法。

If you don't provide a constructor then the compiler plants an implicit default constructor for you: 如果不提供构造函数,则编译器将为您植入一个隐式默认构造函数:

Wand::Wand()
  : mouseEmu()
  , pt()
{}

Thous when you create an object of Wand. 创建魔杖对象时,数量为千。 It will automatically create and initialize its members (mouseEmu and pt) as part of the construction of the Wand object. 作为Wand对象构造的一部分,它将自动创建并初始化其成员(mouseEmu和pt)。 Calling their constructors (in the order declared in the class). 调用其构造函数(按类中声明的顺序)。

Note: If you define your own constructor. 注意:如果定义自己的构造函数。 But don't explicitly call the constructor of your members then the members default constructor are implicitly called (the order of construction of members is ALWAYS the order of declaration. 但是不要显式调用成员的构造函数,然后隐式调用成员的默认构造函数(成员的构造顺序始终是声明的顺序。

 // Example:
 // If you did:

 class Wand
 {
      Wand(int x) : mouseEmu(0,x) {}

      // The above will generate this logical code.
      Wand(int x) : mouseEmu(0,x), pt() {}
 }

That's not legal C++ syntax. 那不是合法的C ++语法。 It looks like you're declaring a function, but 0 is not a valid type identifier. 您似乎在声明一个函数,但是0不是有效的类型标识符。

Constructors of subobjects (bases and member variables) are called from the constructors of your class, including compiler-generated constructors. 从类的构造函数(包括编译器生成的构造函数)中调用子对象的构造函数(基和成员变量)。

The ctor-initializer-list allows you to specify parameters to subobject constructors. ctor-initializer-list允许您为子对象构造函数指定参数。 It looks like this: 看起来像这样:

class Wand
{
    mouse mouseEmu;

    QCursor pt;
public:
    Wand() : mouseEmu(0,0) {}
};

This way, your mouseEmu subobject is initialized with a two-parameter constructor, and pt is initialized by its default (zero-parameter) constructor. 这样,您的mouseEmu子对象将使用两参数构造函数初始化,而pt将通过其默认(零参数)构造函数初始化。

The compiler-generated copy-constructor will initialize both subobjects by calling copy-constructors and passing the members of the Wand object being copied. 编译器生成的复制构造函数将通过调用复制构造函数并传递要复制的Wand对象的成员来初始化两个子对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM