Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;方法继承帮助_C++_Inheritance_Polymorphism_Overloading - Fatal编程技术网

C++ C++;方法继承帮助

C++ C++;方法继承帮助,c++,inheritance,polymorphism,overloading,C++,Inheritance,Polymorphism,Overloading,我有一个Graphic类,它处理项的绘制,因此我的所有项类都将继承它 本质上,我有一个Graphic::Draw(type argX,type argY)方法,它要求argX和argY通过Item方法传递给它 所以我想我可以很容易地得到一个Item::DrawMe()方法,如下所示: class Graphic { ... void Draw(type argX, type argY) { // drawing code } ... }

我有一个
Graphic
类,它处理
项的绘制,因此我的所有
类都将继承它

本质上,我有一个
Graphic::Draw(type argX,type argY)
方法,它要求
argX
argY
通过
Item
方法传递给它

所以我想我可以很容易地得到一个
Item::DrawMe()
方法,如下所示:

class Graphic {
    ...
    void Draw(type argX, type argY)
    {
        // drawing code
    }
    ...
}


class Item : public Graphic {
    ...
    type mX;
    type mY;
    ...
    void DrawMe() : Draw(this->mX, this->mY) { }
    ...
};
但是我宁愿使用名为
Item::Draw()
Item::Draw()方法。但是,然后
Item::Draw()
方法将与
Graphic::Draw
方法具有相同的名称。但是我不能使用
virtual
base方法,因为我不想明显地覆盖函数体


那我该怎么做呢?谢谢。

没有办法,因为您需要为每个子函数使用不同的功能
绘制
,如果您不能使用
虚拟
,则必须为父函数和子函数分别选择名称。只需命名父函数
DoDraw
和子函数
Draw

您就可以定义子函数调用Draw的范围,以显式调用Graphic::Draw(type,type)。。。例如:

class Graphic {
    ...
    virtual void Draw(type argX, type argY)
    {
        // drawing code
    }
    ...
}


class Item : public Graphic {
    ...
    type mX;
    type mY;
    ...
    virtual void Draw() { Graphic::Draw( this->mX, this->mY ); }
    ...
};
但是要注意Item::Draw(void)阴影(隐藏)Item::Draw(type,type)。您不会在子类中同时看到这两个重载(例如,对象)。您必须使用显式作用域。。。(比如ItemObject->Graphic::Draw(x,y))或者可能是一个“using”子句

此外,您还遇到了多态性方面的编码复杂性。。。通过单个virtual Draw()方法引用一组图形对象很好。例如:第一项;图形*g=&i;g->绘图(foo,bar)

我的建议大致如下:

class Graphic {
    ...
    virtual void Draw(type argX = LOADDATA, type argY = LOADDATA)
    {
       if ( argX == LOADDATA )  argX = getArgX();
       if ( argY == LOADDATA )  argY = getArgY();

        // drawing code
    }
    virtual type getArgX() { return DEFAULT_ARGX; }
    virtual type getArgY() { return DEFAULT_ARGY; }
    ...
}


class Item : public Graphic {
    ...
    type mX;
    type mY;
    ...
    virtual type getArgX() { return mX; }
    virtual type getArgY() { return mY; }
    ...
};

当然,您最好先将类型mX类型mY迁移到基类中。。。大概大多数图形对象都有x,y位置…

您可以毫无问题地命名函数
Item::Draw()
。它不会干扰
Graphic::Draw(type,type)
,因为即使函数具有相同的名称,它们也具有不同的签名(即不同的参数)。如果方法签名不同,即参数不同,则将多个方法命名为同一事物没有什么错。在本例中,
Graphic::Draw()
Item::Draw()
方法具有不同的参数,因此没有问题。此外,您正在使用构造函数初始化列表语法调用基类函数
Draw
,这是无效的语法。您需要在函数体中调用基类
Draw
(即
{
}
之间)才有意义,谢谢!伙计们,如果他们有相同的签名就不会有问题了。。。他们在不同的班级。。。(尽管不管签名是否匹配,都会有阴影(隐藏)问题…可能还有编译器警告…)这看起来也相当不错。不过,我还是要支持OP评论中提到的内容。但这仍然是有用的信息,谢谢。