简体   繁体   English

将基类构造函数复制到子类?

[英]Duplicating base-class constructors to subclass?

I have a large set classes which I need to "wrap" in a very thin subclass. 我有一个大型集合类,需要将其“包装”在一个非常薄的子类中。 The functionality of the base classes doesn't change, and their interface remains intact. 基类的功能不变,并且它们的接口保持不变。

The problem is, that in order to use the base classes's constructors (and most of them have more than one), I need to decalre an identical constructor in each of the subclasses, and simply pass the parameters over to the constructor of the base class (99% of the time I have nothing else to do upon construction). 问题是,为了使用基类的构造函数(它们中的大多数都具有多个),我需要在每个子类中分解一个相同的构造函数,然后将参数简单地传递给基类的构造函数(99%的时间,我在构建时无事可做)。 That's a lot of pointless work. 这是很多没有意义的工作。

What's the easiest way to do this? 最简单的方法是什么?

Edit: Even if it's not natively supported in the language, maybe there's some tool that can auto-generate that code? 编辑:即使语言本身不支持它,也许有些工具可以自动生成该代码?

You could add templated constructors for all possible parameters to your wrapper class: 您可以将所有可能参数的模板化构造函数添加到包装类:

template<class Base>
class wrapper : public Base {
public:
  wrapper() : Base() {}

  template<typename T1>
  wrapper(T1 a1) : Base(a1) {}

  template<typename T1, typename T2>
  wrapper(T1 a1, T2 a2) : Base(a1, a2) {}

  // ...
};

Since the templates will not be instantiated for constructors that aren't called it doesn't matter if these other constructors would be invalid. 由于不会为未调用的构造函数实例化模板,因此这些其他构造函数是否无效也无关紧要。 Only the constructors that are actually used need to exist in the base class: 只有实际使用的构造函数才需要存在于基类中:

class A {
public:
  A(int a) {};
};

int main() {
   wrapper<A> aw(1);
   return 0;
}

Probably there are some corner cases where this approach will lead to problems, but for simple cases it works. 在某些极端情况下,这种方法可能会导致问题,但是对于简单情况,它是可行的。

I think you're out of luck unless you can use C++0x. 我认为除非您可以使用C ++ 0x,否则您很不走运。 Read this article: 阅读本文:

Automatic creation of constructor, based on parent class' constructor (C++) 根据父类的构造函数自动创建构造函数(C ++)

Wishful thinking leads me to suggest 一厢情愿的想法促使我提出建议

class BaseClass {
public:
    BaseClass(foo, bar, baz);
};

template <class T>
class BaseInheritor : public BaseClass {
public:
    T(foor, bar, baz);
};

class Child : public BaseInheritor<Child> {
    // Other stuff
};

I don't think C++ templates can quite do what I've just written, unfortunately. 不幸的是,我认为C ++模板不能完全完成我刚才写的工作。 There's a pretty straightforward answer with macros, but I feel dirty suggesting it: 宏有一个非常简单的答案,但是我建议这样做很脏:

#define CHILD_CLASS_DEFAULT(name) \
class name : public BaseClass { \
    public: name(foo, bar, baz) : BaseClass(foo, bar, baz) { } \
    private:


#define CHILD_CLASS_OVERRIDE(name) \
class name : public BaseClass { \

I'd go for a script-based solution, maybe based on gccxml or something similar. 我会选择基于脚本的解决方案,也许基于gccxml或类似的东西。
With that you could write a simple script (ie generate-derivate <input-file> <base-name> <derivative-name> <output-file> ) that generates you the wrapper. 这样,您可以编写一个简单的脚本(即generate-derivate <input-file> <base-name> <derivative-name> <output-file> )来生成包装器。 You'd only have to make changes manually if you are adding/changing functionality. 如果要添加/更改功能,则只需手动进行更改。

edit: vague outline: 编辑:模糊的轮廓:

write derivative-name + " : public " + base-name + " {\npublic:\n"
foreach <Constructor> where attribute name == base-name
    write "\tderivate-name("
    foreach <Argument>
       resolve by attribute type (query FundamentalType, ReferenceType, ...)
       write argument type etc.
    write ");\n"
write "\n};\n"

It all depends on what script language and what xml tools you use. 这完全取决于您使用的脚本语言和XML工具。 Trolltech has XQuerys in their example . Trolltech 的示例中包含XQuerys。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM