【问题标题】:How to use a class with a constructor in another class?如何在另一个类中使用带有构造函数的类?
【发布时间】:2023-03-16 09:04:01
【问题描述】:

C++ 中的新功能,我想在另一个类中使用一个带有构造函数的类,并在全局范围内使用它的方法。

MyMachine 类调用 MyComponent 类。如果 MyComponent 没有构造函数,它可以工作,但我不知道如何使用构造函数调用它。

工作:

#include <iostream>

/*********************************************
* MyComponent
*********************************************/

// MyComponent.h
class MyComponent
{
  public:
    int _id;
    MyComponent();
    int getId();
};

// MyComponent.cpp
MyComponent::MyComponent()
{

}

int MyComponent::getId()
{
  return _id;
}

/*********************************************
* MyMachine
*********************************************/

// MyMachine.h
class MyMachine
{
  private:
    MyComponent component;
  public:
    MyMachine();
    void printComponentInfo();
};

MyMachine::MyMachine()
{
  component._id = 123456;
}

void MyMachine::printComponentInfo()
{
  int id = component.getId();
  std::cout << id << "\n";
}

/*********************************************
* Main
*********************************************/

int main()
{
  MyMachine machine;
  machine.printComponentInfo();

return 0;
}

显示 123456

但是使用构造函数,它不起作用:

#include <iostream>

/*********************************************
* MyComponent
*********************************************/

// MyComponent.h
class MyComponent
{
  public:
    int _id;
    MyComponent(int id);
    int getId();
};

// MyComponent.cpp
MyComponent::MyComponent(int id)
{
    _id = id;
}

int MyComponent::getId()
{
  return _id;
}

/*********************************************
* MyMachine
*********************************************/

// MyMachine.h
class MyMachine
{
  private:
    MyComponent component(int id);
  public:
    MyMachine();
    void printComponentInfo();
};

MyMachine::MyMachine()
{
  component._id = 123456;
}

void MyMachine::printComponentInfo()
{
  int id = component.getId();
  std::cout << id << "\n";
}

/*********************************************
* Main
*********************************************/

int main()
{
  MyMachine machine;
  machine.printComponentInfo();

return 0;
}

抛出错误

all.cpp:在构造函数“MyMachine::MyMachine()”中:all.cpp:43:7: 错误: 无效使用成员函数‘MyComponent MyMachine::component(int)’ (你忘了'()'吗?) 组件._id = 123456; ^~~~~~~~~ all.cpp:在成员函数'void MyMachine::printComponentInfo()'中:all.cpp:48:16:错误:无效使用 成员函数‘MyComponent MyMachine::component(int)’(你 忘记“()”?) int id = component.getId();

【问题讨论】:

标签: c++ class object constructor


【解决方案1】:

你有两个错误:

  • 缺少MyComponent 类的默认构造函数:
MyComponent() : _id(0) {}
  • MyMachine 类中的私有数据成员声明错误:
class MyMachine {
private:
    MyComponent component;
    // ...
};

【讨论】:

  • 谢谢它有效。我只是想知道如何在 MyMachine 中使用选定的 int 参数调用 MyComponent() 构造函数。这可能吗?
  • @Phoax 当然有可能。你可以拥有像MyMachine::MyMachine() : component(1) {} 这样的MyMachine 构造函数。这里调用了MyComponent 构造函数。如果回答对你有帮助,请采纳。
  • @Phoax 或者你甚至可以做MyMachine::MyMachine(int id) : component(id) {...}
【解决方案2】:

我认为您的问题在于理解何时调用构造函数。

当您构造MyMachine 的新对象时,代码首先初始化MyMachine 的所有成员进入构造函数的主体。在进入 ctor 主体之前,您可以在 initializer list 中自行初始化成员:

MyMachine::MyMachine(int id) :
    _id(id)
{
}

其实这是一种更高效的初始化方式,因为所有成员必须在ctor体之前初始化,因此,在之前的状态下,代码会:

  1. 使用其无参数 ctor 初始化 _id
  2. 将值id 放入MyComponent。

当您使用初始化器列表时,初始化过程中只有 一个 步骤,效率更高(尤其是在更复杂的类型中!)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2017-02-25
    • 2017-12-31
    • 2014-08-01
    • 1970-01-01
    相关资源
    最近更新 更多