在C ++中的一些其他指令之后调用基类的构造函数 [英] Calling the constructor of the base class after some other instructions in C++

查看:104
本文介绍了在C ++中的一些其他指令之后调用基类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,不可能调用基类的构造函数。我知道的唯一方法是:

  MyClass :: MyClass(/ * args * /):Base(/ * args * /)
{
// ...
}

但这将在开始时调用构造函数。
有没有办法在构造函数中的其他地方调用它?类似这样的:

  MyClass :: MyClass(/ * args * /)
{
// ...说明
Base :: Base(/ * args * /);
// ... other_instructions
}

根据 C ++超类构造函数调用规则问题我明白没有办法,但我阅读这里,我猜这是可能的,但如果我尝试我得到:

 错误:无效使用'Base'类。 

我做错了什么?



谢谢!



编辑:我明白我忘了一个关键点:基类是框架的一部分,因此如果可能的话,不必修改它是很好。

解决方案

如果基类构造函数需要至少一个参数,可以使用如下所示的辅助函数:

 code> int DoStuffBeforeCtorAndForwardInt(int arg,Foo foo)
{
DoStuff(arg,foo);
return arg;
}

MyClass :: MyClass(int arg,Foo foo)
:Base(DoStuffBeforeCtorAndForwardInt(arg,foo))
{
//。 ..
}



如果你想默认初始化基类,复制一个默认的初始化基类实例的copy-ctor:

  Base DoStuffBeforeCtorAndReturnDefaultBase(int arg,Foo foo)
{
DoStuff(arg,foo);
return Base();
}

MyClass :: MyClass(int arg,Foo foo)
:Base(DoStuffBeforeCtorAndReturnDefaultBase(arg,foo))
{
// ..
}

或者,如果 Base 不必是第一个基类,您可以从辅助类派生 MyClass

  MyClass :: MyClass(/ * ... * /)
:DoStuffHelperClass(/ * ... * /),
Base * /)
{
// ...
}

上述所有要求你所做的东西不依赖于要被初始化的对象(即函数不能安全地是成员函数,你不能安全地传递 this 作为它们的参数)。



这意味着你可以做一些日志记录或类似的,但是你也可以在基类后面除了使用DoStuffHelperClass解决方案,您当然可以在DoStuffHelperClass中拥有成员,访问它们,而不是访问它们)



p>




虽然我不得不说,我不记得曾经使用/需要/想要的东西。很可能有另一个(更好的)解决方案,你想要做的。


As far as I know it is not possible to call the constructor of the base class. The only way I know is this:

MyClass::MyClass(/* args */) : Base(/* args */)
{
   // ...
}

but this would invoke the constructor at the beginning. Is there any way to call it somewhere else in the constructor? Something like this:

MyClass::MyClass(/* args */)
{
   // ... instructions
   Base::Base(/* args */);
   // ... other_instructions
}

According to this C++ superclass constructor calling rules question I understand there is no way but I read here and I guessed it was possible, but if I try I get:

error: invalid use of 'class Base'.

Am I doing something wrong? Is it possible to do this some way or is there any other possible solution to this need?

Thanks!

EDIT: I understand I forgot a key point: the base class is part of a framework, and therefore it would be good not to have to modify it, if possible.

解决方案

If the base class constructor takes at least one argument, you could use a helper function like this:

int DoStuffBeforeCtorAndForwardInt(int arg, Foo foo)
{
    DoStuff(arg, foo);
    return arg;
}

MyClass::MyClass(int arg, Foo foo)
    : Base(DoStuffBeforeCtorAndForwardInt(arg, foo))
{
   // ...
}

If you want to default-initialize the base class, you could use the copy-ctor to copy a default initialized base class instance:

Base DoStuffBeforeCtorAndReturnDefaultBase(int arg, Foo foo)
{
    DoStuff(arg, foo);
    return Base();
}

MyClass::MyClass(int arg, Foo foo)
    : Base(DoStuffBeforeCtorAndReturnDefaultBase(arg, foo))
{
   // ...
}

Or, if Base doesn't have to be the first base class, you could derive MyClass from a helper class:

MyClass::MyClass(/* ... */)
    : DoStuffHelperClass(/* ... */),
    Base(/* ... */)
{
   // ...
}

All of the above require that the "stuff" you do does not depend on the object that's about to be initialized (i.e. the functions can't safely be member functions and you cannot safely pass this as an argument to them either).

That means you can do some logging or similar, but then again you could also do that after the base class has been initialized.

(EDIT except with the DoStuffHelperClass solution, you can of course have members in DoStuffHelperClass, access them and what not)


Although I have to say that I can't recall ever using/needing/wanting something like that. It's quite probable that there is another (preferable) solution for what you're trying to do.

这篇关于在C ++中的一些其他指令之后调用基类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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