非默认构造函数的C ++继承 [英] C++ inheritance with non-default constructors

查看:58
本文介绍了非默认构造函数的C ++继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我正在研究存在以下情况的情况.有一个foo类,在构造时需要一个标识号x.因此,其构造函数参数类似于foo(x).

For a project I am looking at a case where the following situation exists. There is a class foo which requires an identifying number x when constructed. So its constructor argument looks something like foo(x).

现在,我想派生一个基于foo的类,称为bar,该类基本上将在内部容纳两个foo,并且在外部世界中将充当单个foo.我要执行此操作的方法是使用bar的两个构造函数参数y和z,然后将在构造函数中生成foo(y)和foo(z).我希望一切都清楚.

Now I want to derive a class based on foo, called bar, which will basically hold two foo's internally and to the outside world acts as a single foo. The way I want to do this is by use of two constructor arguments y and z for bar, which will then, in the constructor, generate foo(y) and foo(z). I hope it's clear.

我当前的实现类似于:

class bar : public foo {
public:
    bar(int y, int z)
    {
        _foo_ptr_1 = foo::foo(y);
        _foo_ptr_2 = foo::foo(z);
    };

private:
    foo *_foo_ptr_1;
    foo *_foo_ptr_2;
};

其中foo是一个类,它是一个已经存在的类,类似于:

Where foo is a class that is an already existing class looking something like:

class foo {
public:
    foo(int x) :
    _private_var(x) {
    };
};

现在,我知道由于foo没有带有零参数的默认构造函数,因此它将不起作用.由于某些原因,我宁愿不向foo添加零参数的无意义构造函数.有没有一种很好的方法来使此工作?现在,我的编译器(在Ubuntu下为gcc)给我以下错误:

Now, I know that since foo has no default constructor with zero arguments this will not work. I would prefer not to add a meaningless constructor with zero arguments to foo for several reasons. Is there a nice way to get this working? Right now my compiler (gcc under Ubuntu) gives me the following error:

error ::没有匹配函数可调用foo :: foo()

error::no matching function for call to foo::foo()

尝试解决此问题的快速尝试是替换bar的构造函数:

A quick attempt to try and work around this is by replacing the constructor of bar:

bar(int y, int z) : foo(y)

但这也不起作用.

我还无法在线找到一个可行的解决方案,因此非常感谢您的帮助.

I have not been able to find a working solution for this yet online, and would be gratefull for any help.

推荐答案

对我来说这很好:

bar(int y, int z) : foo(y)
{
  _foo_ptr_1 = new foo(y);
  _foo_ptr_2 = new foo(z);
}

请不要试图像静态方法那样直接调用类构造函数并不是在C ++中构造类的方式.至少,您如上所述使用 new classname(arguments).

Please not that trying to invoke a class constructor directly like it was a static method is not how classes are constructed in C++. At the very least, you use new classname(arguments), as above.

这不是真正的建议,但是...使用 new delete 进行显式的内存管理很容易出错.

This is not really recommended however... explicit memory management using new and delete is easy to get wrong.

首先,考虑到您可能根本不必分配任何 foo 实例..您可以只具有简单的成员变量:

First, consider that you may not have to allocate any foo instances at all.. you can just have simple member variables:

class bar : public foo {
public:
  bar(int y, int z) : foo(y), _foo_1(y), _foo_2(z)
  {
  }

private:
  foo _foo_1;
  foo _foo_2;
};

如果您真的必须以困难的方式构造新实例,则应该考虑使用智能指针,例如 unique_ptr .

If you really must construct new instances the hard way, you should consider using smart pointers such as unique_ptr.

class bar : public foo {
public:
  bar(int y, int z) : foo(y), _foo_ptr_1(new foo(y)), _foo_ptr_2(new foo(z))
  {
  }

private:
  std::unique_ptr<foo> _foo_ptr_1;
  std::unique_ptr<foo> _foo_ptr_2;
};

unique_ptr 可确保您的资源在其所有者被销毁时被正确地重新分配.如果您的 _foo 成员变量不完全属于其父 bar 实例,则可以改用 shared_ptr .值得一读这些实用程序类的使用.

unique_ptr ensures that your resources are correctly deallocated when their owner is destroyed. If your _foo member variables don't belong exclusively to their parent bar instance, then you can use shared_ptr instead. It is well worth reading up on the use of these utility classes.

这篇关于非默认构造函数的C ++继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆