不调用基类构造函数 [英] Not calling base class constructors

查看:91
本文介绍了不调用基类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在构建一个子类时,基类得到''

它的控制器被调用。


有没有有什么方法可以避免这个?基类有一个变量,

初始化为0.


子类的构造函数将此变量设置为实数值。


所以首先将变量设置为0,变量从未在它之前使用

再次设置为实际值。这是浪费。有没有想法?

I''ve noticed that when constructing a subclass, the base class get''s
it''s contructors called.

Is there some way to avoid this? The base class has one variable, which
gets initialised to 0.

The subclass''s constructor sets this variable to a real value.

So first the variable is set to 0, the variable is never used before it
once again gets set to a real value. It''s a waste. Any ideas anyone?

推荐答案



< st ****** @ hotmail.com> ;在消息中写道

news:11 ********************* @ g43g2000cwa.googlegro ups.com ...

<st******@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
我注意到在构建子类时,基类得到了''它''的结构调用。

有什么方法可以避免这种情况吗?基类有一个变量,它被初始化为0.

子类的构造函数将此变量设置为实际值。

首先,变量设置为0,变量在它再次被设置为实际值之前从未使用过。这是浪费。任何人的想法?
I''ve noticed that when constructing a subclass, the base class get''s
it''s contructors called.

Is there some way to avoid this? The base class has one variable, which
gets initialised to 0.

The subclass''s constructor sets this variable to a real value.

So first the variable is set to 0, the variable is never used before it
once again gets set to a real value. It''s a waste. Any ideas anyone?



你可以尝试类似以下的东西:


class Base {

int mem;

public:

Base(int val = 0):mem(val){}

};


class派生:public Base {

public:

派生(int a = 0):Base(a){}

};


问候,

Sumit。

-

Sumit Rajan< su * ***@msdc.hcltech.com>


问候,

Sumit。

-

Sumit Rajan su****@msdc.hcltech.com


st******@hotmail.com 写道:
我注意到在构造子类时,基类得到了'''它的构造函数被调用。


是的,这应该是在

派生之前实例化的基础上发生的。

有什么方法可以避免这个?基类有一个变量,它初始化为0.


编号

子类的构造函数将此变量设置为a真正的价值。


为什么基类构造函数没有设置它?派生的

类如何设置它 - 我希望用setter。我们不希望受保护的成员

:-)。

首先将变量设置为0,变量从未在它之前再次使用
设置为实际值。这是浪费。任何人的想法?
I''ve noticed that when constructing a subclass, the base class get''s
it''s contructors called.
Yes, that is supposed to happen as the base is instantiated before the
derived.

Is there some way to avoid this? The base class has one variable, which
gets initialised to 0.
No.

The subclass''s constructor sets this variable to a real value.
Why does the base class constructor not set it? How does the derived
class set it - with a setter, I hope. We don''t want protected members
:-).

So first the variable is set to 0, the variable is never used before it
once again gets set to a real value. It''s a waste. Any ideas anyone?




通过将参数转发给基础构造函数来初始化它。


class Base

{

public:

explicit Base(int mem):mem_(mem){; }

//其余的基础接口...省略 - 可能包括

//虚拟〜基础,取决于是否要删除

//通过Base。

int getMem()const;

void setMem();


private:

int mem_;

};


class派生:公共基地

{

public:

显式派生(int mem):Base(mem){}

//等等......

};



Initialise it by forwarding the parameter to the base constructor.

class Base
{
public:
explicit Base(int mem): mem_( mem ){ ; }
//The rest of bases interface...omitted - perhaps includes
// virtual ~Base, depending on whether want to delete
// via Base.
int getMem() const;
void setMem();

private:
int mem_;
};

class Derived : public Base
{
public:
explicit Derived( int mem ): Base( mem ){}
//etc...
};


好的,我希望它能像这样工作:


GenericObject obj; //内部成员是0.

StringObject str =" hello world" ;; //字符串对象是
的子类
GenericObject


基本上,StringObject需要通过构造函数构造

而不是通过二传手。否则它会很尴尬。


但是这样做会将它的内部成员设置两次,一次设置为0,然后设置为
然后第二次设置为真实指针。

OK I''d like it to work like this:

GenericObject obj; // internal member is 0.
StringObject str = "hello world"; // string object is a subclass of
GenericObject

So basically, StringObject needs to be constructed via the constructor
and not via a setter. Otherwise it will be awkward.

However doing this will set it''s internal member twice, once to 0 and
then the second time to a real pointer.


这篇关于不调用基类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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