派生类的构造函数 [英] Constructor of a derived class

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

问题描述

父类是一个抽象类,默认隐式构造函数:


----------------------- ------------

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

解决方案

Manuel写道:

父类是一个抽象类,默认隐式构造函数:

-----------------------------------
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);
虚拟空白setHeight(GLint h);
虚拟bool isMouseOver(GLint posX,GLint posX);
虚拟空白绘制();
};
------------------------------------

此外,请注意我在不同的函数中使用相同的变量名称
(posX和posY都在构造函数和setPosition中,w和h在
构造函数,setWidth和setHeight中。但是它们应该没问题,因为它们是
它们懒散地进入职能部门。 ..也没关系?




你在这里大多没事。完全使用相同名称的变量

不同的范围完全没问题。您不应该将派生类中的

方法声明为虚拟,除非您打算继续从它继承
。如果你这样做,你将需要使析构函数

虚拟化。一个好的编译器将在这些

行(虚拟方法但非虚拟dtor)上提供警告。


如果你没有明确地调用基数类构造函数(作为你的ctor'初始化列表中的第一个

项),默认值自动调用

。这就是你想要的,所以你很好。


isMouseOver()和draw()应该都是const。


Luke


Manuel写道:

父类是一个抽象类,默认隐式构造函数:

-----------------------------------
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(){}
};


看起来不错。除了我可能会''isMouseOver''''const''

函数:


虚拟bool isMouseOver(GLint,GLint)const = 0 ;


除非,当然,它出于某种原因修改了对象。

---------------- --------------------

我编写的派生类使用了一个简单的构造函数,因为
父类具有默认构造函数。没有析构函数。没关系?


好​​吧,它不是没有析构函数。它有编译器提供的d-tor,

而d-tor是虚拟的。

----------------- -------------------
班级广场:公共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);
virtual void setHeight(GLint h);
virtual bool isMouseOver(GLint posX,GLint posX);


关于它的常量的相同注释。

虚拟空白绘制();
};
------ ------------------------------

此外,请注意我在不同的地方使用相同的变量名称函数
(posX和posY都在构造函数和setPosition中,w和h在
构造函数,setWidth和setHeight中。但是它们应该没问题,因为它们可以在函数中使用它们。 。也没关系?




是的,参数名称是函数的本地名称。你可以有相同的名字

的参数不同的功能,他们不会互相干扰。


V


Victor Bazarov写道:
< blockquote class =post_quotes>看起来很好。除了我可能会''isMouseOver''''const''




谢谢(对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 the
parent 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 in
constructor, setWidth and setHeight. However they should be ok, because
they work loacally into the functions...it''s ok too?

thanks,

Manuel

解决方案

Manuel wrote:

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 the
parent 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 in
constructor, setWidth and setHeight. However they should be ok, because
they work loacally into the functions...it''s ok too?



You''re mostly okay here. Using variables of the same name in totally
different scopes is no problem at all. You should not declare the
methods in the derived class to be virtual, unless you intend to
inherit further from it. If you do, you''ll need to make the destructor
virtual as well. A good compiler will provide a warning along these
lines (virtual methods but non-virtual dtor).

If you don''t explicitly call the base class constructor (as the first
item in your ctor''s initializer list), the default one gets called
automatically. That''s what you want, so you''re fine.

isMouseOver() and draw() should probably both be const.

Luke


Manuel wrote:

The parent is an abstract class, with default implicit constructor:

-----------------------------------
class mhwidget
{
public:
virtual void draw()= 0;
Shouldn''t ''draw'' have an argument? Maybe not...
virtual void setPosition(GLint, GLint)= 0;
virtual void setWidth(GLint)= 0;
virtual void setHeight(GLint)= 0;
virtual bool isMouseOver(GLint, GLint)= 0;
virtual ~mhwidget() {}
};
Looks fine. Except that I would probably make ''isMouseOver'' a ''const''
function:

virtual bool isMouseOver(GLint, GLint) const = 0;

unless, of course, it modifies the object for some reason.
------------------------------------

The derived class I''ve written use a simple constructor, because the
parent have the default constructor. And without destructor. It''s OK?
Well, it''s not without the destructor. It has compiler-provided d-tor,
and that d-tor is virtual.
------------------------------------
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);
Same note about the constness of it.
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 in
constructor, setWidth and setHeight. However they should be ok, because
they work loacally into the functions...it''s ok too?



Yes, argument names are local to the functions. You can have same names
of arguments in different functions, they won''t interfere with each other.

V


Victor Bazarov wrote:

Looks fine. Except that I would probably make ''isMouseOver'' a ''const''



Thanks (to Luke too).
Now a very beginner question (sorry but this must be so basic that no
one of text I''ve talk about it...).

Without constructor I can create and use the square directly, in example
to push it into a container:

contn.addWidget(new square);

now, with constructor, should I write

contn.addWidget(new square(10,10,100,100));

??
thx,

Manuel


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

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