简体   繁体   English

将默认构造函数更改为参数化

[英]Changing default constructor to parameterized

I had a class "SetupModel" with default constructor. 我有一个带有默认构造函数的“ SetupModel”类。 Now I have to include one parameter to this constructor. 现在,我必须在此构造函数中包含一个参数。 So it should be as, 所以应该这样

class SetupModel
{
public:
   SetupModel(MyData::ValueSystem& valueSystem);

So I am trying to change all references of this class SetupModel. 因此,我试图更改此类SetupModel的所有引用。

But in another one class this has been called as, 但是在另一类中,这被称为

class  SetupManager
{
 private:
   SetupModel _model;

am getting error "no appropriate default constructor available" 出现错误“没有合适的默认构造函数”

How can I change this? 我该如何更改?

class  SetupManager
{
  SetupManager(...) : _model(valuesystemarg), ...
  {
     ...
  }
 private:
   SetupModel _model;

Use initializer list to initialize this member. 使用初始化程序列表初始化此成员。

Or add the default parameterless constructor to SetupModel constructor. 或将默认的无参数构造函数添加到SetupModel构造函数。

add a definition of default constructor 添加默认构造函数的定义

class SetupModel
{
public:
   SetupModel(MyData::ValueSystem& valueSystem);
   SetupModel();
}

You have 2 options in hand 您手边有2个选项

  1. If you prefer to use default constructor without parameters to be used during construction of SetupManager you can define one more constructor within SetupModel without parameters 如果你喜欢使用默认的构造函数没有施工的过程中使用的参数SetupManager ,你可以定义中的一个多个构造SetupModel不带参数
class SetupModel
{
public:
   SetupModel(MyData::ValueSystem& valueSystem);
   SetupModel() {};

So you don't need to make change in SetupManager . 因此,您无需在SetupManager进行更改。

  1. If you prefer to use SetupModel constructor with parameters as base class constructor of SetupManager , you can use initialization list within constructor of SetupManager . 如果您更喜欢使用带有参数的SetupModel构造函数作为SetupManager基类构造函数,则可以在SetupManager构造函数中使用初始化列表。 Then you dont need further change in SetupModel 然后,您无需在SetupModel进一步更改
class  SetupManager
{
    SetupManager(...) : _model(valuesystemarg), ...
    {
       ...
    }
private:
     SetupModel _model;

You can have as many constructors as you want but if what you want is to have only one then you can set the default value both in .h or in .cpp 您可以根据需要拥有任意数量的构造函数,但是如果想要的只是一个构造函数,则可以在.h或.cpp中设置默认值。

in .h: 在.h中:

class  SetupManager
{
    SetupManager() : _model(value), // here you can set all variables u want    separated by a coma

   private:
     SetupModel _model;
}

in cpp: 在cpp中:

SetupManager::SetupManager():
{
   _model = value;
}

Be careful so in .h the value is inside "(" and ")" while in cpp you use "=" to assign the value 请注意,在.h中,该值位于“(”和“)”内,而在cpp中,您使用“ =”来分配该值

暂无
暂无

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

相关问题 带默认值的参数化构造函数是什么意思? - What does it mean by parameterized constructor with default value? C++ 父 Class 调用默认构造函数而不是参数化构造函数 - C++ Parent Class Default Constructor Called Instead of Parameterized Constructor 如何在同一个类中同时具有参数化构造函数和默认构造函数? - How can I have both a parameterized and a default constructor in the same class? 构造函数在默认,参数化,复制ctor和赋值运算符的情况下的行为 - Constructor behavior in case of default, parameterized, copy ctor and assignment operator C ++“没有合适的构造函数可供转换 <default constructor> 参数化构造函数 - C++ "No suitable constructor exists to convert from <default constructor> to parameterized constructor 默认内置构造函数和非参数化用户创建的默认构造函数之间有什么区别吗? - Is there any difference between default built-in constructor and non-parameterized user-created default constructor? 如何在 C++ 的类的默认构造函数中调用成员 object 变量的参数化构造函数? - How to call parameterized constructor of member object variable in a class' default constructor in C++? 为什么即使在调用参数化构造函数时也会调用默认构造函数? - Why does default constructor is called even when I am calling the parameterized constructor? 更改默认的复制构造函数C ++ - Changing the default copy constructor C++ 如何动态地在参数化构造函数中? - How dynamically in the parameterized constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM