如何在不调用 C++ 中的构造函数的情况下分配实例变量?
基本上,我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用构造函数初始化列表:
Use the constructor initialization list:
在构造函数中使用member-initialization-list:
实际上,在您的代码中,所有成员变量都使用赋值而不是各自的构造函数,这意味着
mux< /code> 尝试使用默认构造函数进行构造,甚至在进入
MuxPath
的构造函数之前也是如此。由于VisaMux
没有默认构造函数,因此会出现编译错误。因此,通过使用初始化列表,其中语法
mux(Mux)
调用VisaMux
的复制构造函数,您可以避免调用VisaMux 的默认构造函数
不存在。而且由于mux
已经是复制构造的,因此无需在构造函数主体中使用赋值。Use member-initialization-list in the constructor as:
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 ofMuxPath
. 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 ofVisaMux
, you avoid the invocation of default constructor ofVisaMux
which doesn't exist. And since themux
is already copy-constructed, there is no need to use assignment in the constructor body.它称为“初始化列表”。
It's called "initialization list".
您有点想问如何在不先安装墙钉的情况下在您的房子里获得红色的墙壁。如果您的
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 aMux
variable, at some point during its construction, it's going to need to instantiate a variable of typeMux
. This implies that an instance of typeMux
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.