如何在不调用 C++ 中的构造函数的情况下分配实例变量?

发布于 2024-11-25 21:23:45 字数 905 浏览 0 评论 0原文

基本上,我有一个名为 VisaMux 的类和一个名为 MuxPath 的类。 MuxPath 有一个 VisaMux 私有实例变量。我希望 MuxPath 的构造函数将实例变量分配给给定的 VisaMux 对象,而不调用空的 VisaMux() 构造函数。

5  MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux){
6      clk_sel = Clk_sel;
7      lane_sel = Lane_sel;
8      mux = Mux;
9  }

此代码导致错误:

MuxPath.cpp:5: error: no matching function for call to ‘VisaMux::VisaMux()’
VisaMux.h:20: candidates are: VisaMux::VisaMux(const std::string&, const uint&, const uint&, const std::vector<VisaLane, std::allocator<VisaLane> >&, const std::vector<VisaResource, std::allocator<VisaResource> >&)

如您所见,它在第一行(第 5 行)出错,因此似乎以某种方式 const VisaMux& Mux 正在调用 VisaMux(),但它不存在。如果我只进行 VisaMux Mux,也会发生这种情况。

我不希望它为 VisaMux 调用空构造函数,因为我希望仅通过向其构造函数传递所有必要的参数来创建 VisaMux。

我该怎么做?

Basically, I have a class called VisaMux and a class called MuxPath. MuxPath has a VisaMux private instance variable. I want MuxPath's constructor to assign the instance variable a given VisaMux object without invoking an empty VisaMux() constructor.

5  MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux){
6      clk_sel = Clk_sel;
7      lane_sel = Lane_sel;
8      mux = Mux;
9  }

This code results in the error:

MuxPath.cpp:5: error: no matching function for call to ‘VisaMux::VisaMux()’
VisaMux.h:20: candidates are: VisaMux::VisaMux(const std::string&, const uint&, const uint&, const std::vector<VisaLane, std::allocator<VisaLane> >&, const std::vector<VisaResource, std::allocator<VisaResource> >&)

As you can see, it errors on the first line (line 5), so it seems that somehow const VisaMux& Mux is invoking VisaMux(), which doesn't exist. This also happens if I just do VisaMux Mux.

I don't want it to call an empty constructor for VisaMux because I want VisaMux to be created only by passing its constructor all the necessary parameters.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鸠魁 2024-12-02 21:23:45

使用构造函数初始化列表:

MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux)
       : clk_sel(Clk_sel)
       , lane_sel(Lane_sel)
       , mux(Mux)
{}

Use the constructor initialization list:

MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux)
       : clk_sel(Clk_sel)
       , lane_sel(Lane_sel)
       , mux(Mux)
{}
德意的啸 2024-12-02 21:23:45

在构造函数中使用member-initialization-list:

MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux) 
   :clk_sel (Clk_sel),lane_sel(Lane_sel),mux(Mux)
{ //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it's called initialization-list

}

实际上,在您的代码中,所有成员变量都使用赋值而不是各自的构造函数,这意味着mux< /code> 尝试使用默认构造函数进行构造,甚至在进入MuxPath的构造函数之前也是如此。由于 VisaMux 没有默认构造函数,因此会出现编译错误。

因此,通过使用初始化列表,其中语法 mux(Mux) 调用 VisaMux 的复制构造函数,您可以避免调用 VisaMux 的默认构造函数 不存在。而且由于 mux 已经是复制构造的,因此无需在构造函数主体中使用赋值

Use member-initialization-list in the constructor as:

MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux) 
   :clk_sel (Clk_sel),lane_sel(Lane_sel),mux(Mux)
{ //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it's called initialization-list

}

Actually, in your code, all the member variables use assignment rather than their respective constructor, which means mux tries to get constructed with default constructor, even before it enters into the constructor of MuxPath. And sinceVisaMux doesn't have default constructor, its giving compilation error.

So by using the initialization-list, in which the syntax mux(Mux) invokes the copy-constructor of VisaMux, you avoid the invocation of default constructor of VisaMux which doesn't exist. And since the mux is already copy-constructed, there is no need to use assignment in the constructor body.

小情绪 2024-12-02 21:23:45
   MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux)
       : mux(Mux)
   {
       clk_sel = Clk_sel;
       lane_sel = Lane_sel;
   }

它称为“初始化列表”。

   MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux)
       : mux(Mux)
   {
       clk_sel = Clk_sel;
       lane_sel = Lane_sel;
   }

It's called "initialization list".

坦然微笑 2024-12-02 21:23:45
class MuxPath {
  MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux)
    : clk_sel(Clk_sel), lane_sel(Lane_sel), mux(Mux) {};
  ...
};
class MuxPath {
  MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux)
    : clk_sel(Clk_sel), lane_sel(Lane_sel), mux(Mux) {};
  ...
};
笨笨の傻瓜 2024-12-02 21:23:45

您有点想问如何在不先安装墙钉的情况下在您的房子里获得红色的墙壁。如果您的 MuxPath 类包含一个 Mux 变量,则在其构造过程中的某个时刻,它将需要实例化一个 Mux 类型的变量。这意味着将创建 Mux 类型的实例,而执行此操作的唯一机制是使用构造函数调用。

这可以是默认构造函数、无参数构造函数、复制构造函数或接受其他参数的构造函数。其他答案显示了如何在成员初始化列表中执行此操作。但无法回避这样一个事实:在某些时候,需要调用一些 Mux 构造函数。

You're kind of asking how you can get red walls in your house without first installing wall studs. If your MuxPath class contains a Mux variable, at some point during its construction, it's going to need to instantiate a variable of type Mux. This implies that an instance of type Mux will be created, and the only mechanism to do that is with a constructor call.

This can either be a default, or no-arg constructor, a copy constructor, or a constructor that takes in some other argument. The other answers show how to do that in the member initialization list. But there's no way to get around the fact that at some point, some contructor for Mux will need to be called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文