子类化并使用基类初始化器

发布于 2024-11-14 08:50:32 字数 484 浏览 4 评论 0原文

我有这个:

class foo {
public:
  int a;
  int b;
  int c;
};

这很好,但我想添加一些运算符重载而不修改类 foo:

class bar: public foo {
  operator==(const bar &x) const {
    return a == x.a;
  }
}

到目前为止还好,但现在我需要使用从另一个类获得的对象数组来初始化 bar。没问题,我可以将所有 foo 变量复制到专用构造函数中的 bar 中:

bar::bar(foo &x) {
  a = foo.a;
  /* etc */
}

但这看起来很混乱,如果 foo 类 over 更新了,那么我也必须更新 bar 。如果 bar 可以自动初始化自己,那就更好了(它永远不会包含自己的数据成员)

I have this:

class foo {
public:
  int a;
  int b;
  int c;
};

Which is fine, but I want to add some operator overloading without modifying class foo:

class bar: public foo {
  operator==(const bar &x) const {
    return a == x.a;
  }
}

Fine so far, but now I need to initialise bar with an array of objects I get from another class. No problem, I can just copy all of the foo variables over to bar in a dedicated constructor:

bar::bar(foo &x) {
  a = foo.a;
  /* etc */
}

But this seems messy, and if class foo over gets updated, then I have to update bar too. Much preferable if bar could automatically initialise itself (it'll never contain its own data members)

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

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

发布评论

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

评论(3

天气好吗我好吗 2024-11-21 08:50:32

这不是像确保 Foo 有一个复制构造函数那么简单吗……

bar::bar(foo &x) 
: foo(x)
{
}

class foo {
foo::foo(foo &x)
{
  a = foo.a; /* etc */
}

};

isn't it as simple as making sure Foo has a copy constructor.......

bar::bar(foo &x) 
: foo(x)
{
}

class foo {
foo::foo(foo &x)
{
  a = foo.a; /* etc */
}

};
零時差 2024-11-21 08:50:32

嗯,很简单:

bar::bar(const foo& x)
  : foo(x)
{
}

只需使用 foo 的复制构造函数即可。 :{ 之间的所有内容都称为初始化列表。您可以直接在其中初始化您的成员和基类。对于 foo,复制构造函数可能如下所示:

foo::foo(const foo& other)
  : a(other.a)
  , b(other.b)
  , c(other.c)
{
}

请注意,我通过 const 引用获取两个参数,这是一个很好的做法,因为您没有接触其他对象。另请注意,大多数情况下,您自己为 foo 编写复制构造函数是不必要的,只有当您的类包含原始指针时(例如 int*)。如果您没有定义复制构造函数而只是对所有数据成员进行浅表复制,则编译器将自行生成复制构造函数。

Well, easy:

bar::bar(const foo& x)
  : foo(x)
{
}

Just use foo's copy constructor. Everything between the : and the { is called the initializer list. You can directly initialize your members and base classes in there. For foo, the copy constructor could look like the following:

foo::foo(const foo& other)
  : a(other.a)
  , b(other.b)
  , c(other.c)
{
}

Note that I take both arguments by const reference, which is a good practice since you aren't touching the other objects. Also note that writing a copy constructor for foo yourself is not necessary most of the time, only when your class contains raw pointers (int* for example). The compiler will generate a copy ctor itself if you don't define one and just make a shallow copy of all data members.

香草可樂 2024-11-21 08:50:32

继承构造函数在 C++03 中无法完成,但可以近似。添加这些构造函数:

template<class T1> bar(const T1& x1) : foo(x1) {}
template<class T1, class T2> bar(const T1& x1, const T2& x2) : foo(x1, x2) {}
template<class T1, class T2, class T3> bar(const T1& x1, const T2& x2, const T3& x3) : foo(x1, x2, x3) {}
// as much as you need...

在 C++0x 中,使用单个可变参数模板构造函数和完美转发。

Inheriting constructors can't be done in C++03, but can be approximated. Add these constructors:

template<class T1> bar(const T1& x1) : foo(x1) {}
template<class T1, class T2> bar(const T1& x1, const T2& x2) : foo(x1, x2) {}
template<class T1, class T2, class T3> bar(const T1& x1, const T2& x2, const T3& x3) : foo(x1, x2, x3) {}
// as much as you need...

In C++0x use a single variadic templated constructor and perfect forwarding.

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