不调用基类构造函数

编程入门 行业动态 更新时间:2024-10-22 11:03:40
本文介绍了不调用基类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我注意到在构建一个子类时,基类得到'' 它的控制器被调用。 有没有有什么方法可以避免这个?基类有一个变量, 初始化为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> ;在消息中写道 news:11 ********************* @ g43g2000cwa.googlegro ups ... <st******@hotmail> wrote in message news:11*********************@g43g2000cwa.googlegro ups... 我注意到在构建子类时,基类得到了''它''的结构调用。 有什么方法可以避免这种情况吗?基类有一个变量,它被初始化为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> 问候, Sumit。 - Sumit Rajan su****@msdc.hcltech

st******@hotmail 写道: 我注意到在构造子类时,基类得到了'''它的构造函数被调用。 是的,这应该是在 派生之前实例化的基础上发生的。 有什么方法可以避免这个?基类有一个变量,它初始化为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.

更多推荐

不调用基类构造函数

本文发布于:2023-10-31 07:35:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!