本文介绍了派生类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 父类是一个抽象类,默认隐式构造函数: ----------------------- ------------ class mhwidget { public: virtual void draw()= 0; virtual void setPosition(GLint,GLint)= 0; virtual void setWidth(GLint)= 0; virtual void setHeight(GLint)= 0; 虚拟bool isMouseOver(GLint,GLint)= 0; virtual~mhwidget(){} }; ------------------------------------ 我编写的派生类使用了一个简单的构造函数,因为 父类具有默认构造函数。没有析构函数。没关系? -------------------------------- ---- 班级广场:公共mhwidget { 私人: GLint positionX; GLint positionY; GLint宽度; GLint高度; public: square( int posX,int posY,GLint w,GLint h); virtual void setPosition(GLint posX,GLint posX); virtual void setWidth(GLint w); 虚拟void setHeight(GLint h); 虚拟bool isMouseOver(GLint posX,GLint posX); virtual void draw(); }; ----------------------------------- - 此外,请注意我在不同的函数中使用相同的变量名称 (posX和posY都在构造函数和setPosition中,w和h在 构造函数,setWidth和setHeight。但是它们应该没问题,因为 它们可以在函数中运行...它也可以吗? 谢谢, 男人uel 解决方案 你在这里大多没事。完全使用相同名称的变量 不同的范围完全没问题。您不应该将派生类中的 方法声明为虚拟,除非您打算继续从它继承。如果你这样做,你将需要使析构函数 虚拟化。一个好的编译器将在这些 行(虚拟方法但非虚拟dtor)上提供警告。 如果你没有明确地调用基数类构造函数(作为你的ctor'初始化列表中的第一个 项),默认值自动调用 。这就是你想要的,所以你很好。 isMouseOver()和draw()应该都是const。 Luke 是的,参数名称是函数的本地名称。你可以有相同的名字 的参数不同的功能,他们不会互相干扰。 V 谢谢(对Luke来说)。 现在这是一个非常初学的问题(对不起,但这必须是基本的,没有 其中一个文本我已经谈过了......)。 没有构造函数我可以直接创建和使用方形,例如 把它推入一个容器: contn.addWidget(新方块); 现在,有构造函数,我应该写 contn.addWidget(新广场(10,10,100,100)); ?? thx, Manuel The parent is an abstract class, with default implicit constructor:-----------------------------------class mhwidget{public:virtual void draw()= 0;virtual void setPosition(GLint, GLint)= 0;virtual void setWidth(GLint)= 0;virtual void setHeight(GLint)= 0;virtual bool isMouseOver(GLint, GLint)= 0;virtual ~mhwidget() {}};------------------------------------The derived class I''ve written use a simple constructor, because theparent have the default constructor. And without destructor. It''s OK?------------------------------------class square : public mhwidget{private:GLint positionX;GLint positionY;GLint width;GLint height;public:square(int posX, int posY, GLint w, GLint h);virtual void setPosition(GLint posX, GLint posX);virtual void setWidth(GLint w);virtual void setHeight(GLint h);virtual bool isMouseOver(GLint posX, GLint posX);virtual void draw();};------------------------------------Besides, note that I use the same variables names in different functions(posX and posY are in both constructor and setPosition, w and h are inconstructor, setWidth and setHeight. However they should be ok, becausethey work loacally into the functions...it''s ok too?thanks,Manuel 解决方案You''re mostly okay here. Using variables of the same name in totallydifferent scopes is no problem at all. You should not declare themethods in the derived class to be virtual, unless you intend toinherit further from it. If you do, you''ll need to make the destructorvirtual as well. A good compiler will provide a warning along theselines (virtual methods but non-virtual dtor).If you don''t explicitly call the base class constructor (as the firstitem in your ctor''s initializer list), the default one gets calledautomatically. That''s what you want, so you''re fine.isMouseOver() and draw() should probably both be const.LukeYes, argument names are local to the functions. You can have same namesof arguments in different functions, they won''t interfere with each other.VThanks (to Luke too).Now a very beginner question (sorry but this must be so basic that noone of text I''ve talk about it...).Without constructor I can create and use the square directly, in exampleto push it into a container:contn.addWidget(new square);now, with constructor, should I writecontn.addWidget(new square(10,10,100,100));??thx,Manuel 这篇关于派生类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 20:32