命名构造函数习惯用法和继承

发布于 2024-08-09 07:03:54 字数 210 浏览 5 评论 0原文

我有一些类,其中有一些 命名构造函数 。当我继承它们时,我应该如何继承构造函数?问题是它们返回基类的对象而不是子类的对象。

附带问题:我可以使用 C++0x “using” 来减少样板代码量吗?

I have a few classes with a few named constructors. When I inherit from them, how should I inherit the constructors? The problem is that they return objects of base class and not of the child class.

Side question: can I use C++0x "using" to reduce the amount of boilerplate code?

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

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

发布评论

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

评论(4

不必了 2024-08-16 07:03:54
struct Foo {
    template<typename T> static T one () { return T(1); }
};

struct A { int x; A(int i) : x(i) {}};
struct B : A { B(int i) : A(i) {}};

这可以让你做类似的事情

A a = Foo::one<A> (); 
B b = Foo::one<B> ();
struct Foo {
    template<typename T> static T one () { return T(1); }
};

struct A { int x; A(int i) : x(i) {}};
struct B : A { B(int i) : A(i) {}};

Which allows you to do things like

A a = Foo::one<A> (); 
B b = Foo::one<B> ();
囚你心 2024-08-16 07:03:54

您既不继承“经典”构造函数,也不继承“命名”构造函数。您应该为每个派生类创建特定的构造函数。

下面是一个示例,说明如何将命名构造函数与继承一起使用:

class SpiralPoint: public Point{
  private: SpiralPoint(float t, float r)
     :Point(Point::polar(r*t, t))  { };
};

Neither do you not inherit "classic" constructors, nor you inherit "named" constructors. You should create specific constructors for each derived class.

Here's an example, how named constructor can be used with inheritance:

class SpiralPoint: public Point{
  private: SpiralPoint(float t, float r)
     :Point(Point::polar(r*t, t))  { };
};
心清如水 2024-08-16 07:03:54

命名 ctor 是一个习语,它们不是真正的构造函数。严格来说,命名构造函数依赖于静态函数。继承需要虚拟函数。现在,非成员不能是虚拟的,因此排除了静态虚拟函数的想法。

附带问题:我可以使用 C++0x “using”来减少样板代码量吗?

using 声明要求编译器继承全部或无基类构造函数。所以,是的,他们可以在某种程度上简化您的代码。但是,你们所有的编译器都支持 C++0x 吗?

Named ctor is an idiom, they are not real constructors. Strictly speaking, named ctors depend on static functions. Inheritance requires virtual functions. Now, non-members cannot be virtual, hence the idea of having static virtual functions is ruled out.

Side question: can I use C++0x "using" to reduce the amount of boilerplate code?

The using declaration asks the compiler to inherit all-or-none of the base class ctors. So, yes in a way they can simplify your code. However, do all your compilers support C++0x?

情深缘浅 2024-08-16 07:03:54

我找到了(几乎)完美的解决方案!

template <class T>
class Color {

 public:

  static T Red   () { return T (0); }
  static T Green () { return T (1); }
  static T Blue  () { return T (2); }

 protected:

  explicit Color (int raw) : raw (raw) {
  }

 private:

  int raw;

};

class MoreColor : public Color <MoreColor> {

 public:

  static MoreColor Octarina() { return MoreColor(8); }

 private:

  friend class Color <MoreColor>;
  explicit MoreColor (int raw) : Color <MoreColor> (raw) {}
};

void Test() {
  MoreColor o = MoreColor::Octarina();
  MoreColor r = MoreColor::Red();
}

它编译:D

I found (almost) perfect solution!

template <class T>
class Color {

 public:

  static T Red   () { return T (0); }
  static T Green () { return T (1); }
  static T Blue  () { return T (2); }

 protected:

  explicit Color (int raw) : raw (raw) {
  }

 private:

  int raw;

};

class MoreColor : public Color <MoreColor> {

 public:

  static MoreColor Octarina() { return MoreColor(8); }

 private:

  friend class Color <MoreColor>;
  explicit MoreColor (int raw) : Color <MoreColor> (raw) {}
};

void Test() {
  MoreColor o = MoreColor::Octarina();
  MoreColor r = MoreColor::Red();
}

And it compiles :D

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