如何包装另一个类(c++)
How to wrap another class (c++)
在用其他面向对象语言编程多年后,我正在努力学习c++。
我正在尝试为另一个类创建一个包装器类,但是很难弄清楚如何正确设置它。
例如,用下面的…
main.cpp
#include "foo.cpp"
#include <iostream>
int main() {
Foo foo(42);
std::cout << foo.get_barx() << std::endl;
return 0;
}
foo.cpp
#include "bar.cpp"
class Foo {
public:
// I'm trying to declare the member variable `m_bar` here. I
// don't want to be instantiating an instance of Bar yet,
// but I think that might be exactly what's happening.
Bar m_bar;
Foo(int y) {
// Here's where I really want to instantiate an instance of Bar
// and assign it to m_bar.
Bar m_bar(y*2);
}
int get_barx() {
return m_bar.getx();
}
};
bar.cpp
class Bar {
public:
int m_x;
// I seem to need this default constructor for the declaration
// of `m_bar` above, but I don't think that line should be
// calling any constructors.
Bar() { m_x = 21; };
Bar(int x) {
m_x = x;
}
int getx() {
return m_x;
}
};
当我编译并运行它时,我得到了21,但我期望得到84。我很确定我做了一些根本性的错误,我很确定这与我如何在Foo中声明m_bar
成员变量有关,但我无法找出正确的方法来完成这一点。
main.cpp
#include "foo.cpp"
#include <iostream>
int main()
{
Foo foo(42);
std::cout << foo.get_barx() << std::endl;
return 0;
}
这里你应该包含一个头文件(例如,将"foo.cpp"重命名为"foo.h")。一般来说,头文件提供声明,而源文件(例如,.cpp文件)提供定义/实现。
Bar.cpp(同样应该是标题)
class Bar
{
public:
int m_x;
// A default constructor is not required, however defining any constructor
// prevents auto generation of the default constructor
Bar(int x) : // This starts the initializer list section
m_x(x)
{
// This is assignment not initialization
// m_x = x;
}
// See the trailing 'const', research const correctness
int getx() const
{
return m_x;
}
};
foo.cpp(同样应该是头文件)
#include "bar.cpp"
class Foo
{
public:
// Just declaring a `Bar` data member
Bar m_bar;
Foo(int y) :
m_bar(y) // Initialize `Bar` data member using the available constructor
{
// First, this declares a new `Bar` instance which is different than
// the class member, regardless of the fact they are named the same
// Bar m_bar(y*2);
// Furthermore, even if you did the following it is assignment not initialization
// m_bar = Bar(y*2);
// Since initialization already occurred before this point an error
// will result if the `Bar` data member isn't instantiated via the
// only available constructor, since there isn't a default constructor as
// explained above
}
// Same comment about const correctness
int get_barx() const
{
return m_bar.getx();
}
};
您需要在构造时使用初始化列表来构造类成员。构造函数的主体在构造完所有成员后调用。如果你没有显式地调用非默认构造函数,那么编译器会为你插入对默认构造函数的调用。例如,您的Foo
类可能如下所示:
class Foo {
public:
Foo(int y) : m_bar(y*2) {}
...
private:
Bar m_bar;
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- C++试图读取一个文件并输出到另一个文本文件
- 如何将指针从一个void函数传递到另一个C++
- 如何从另一个文件继承私有成员变量和公共函数
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 如何在C++中将函数发送到另一个进程
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口