【问题标题】:c++ does virtual function always got to be a const?c++ 虚函数总是必须是常量吗?
【发布时间】:2013-11-17 20:23:01
【问题描述】:

我只是想知道是否所有的虚函数都必须是常量?

我遇到了一些问题,因为当我想打印它们时,该区域总是为我的正方形返回 0。如果有人能启发我,将不胜感激。

shapetwod.h

class ShapeTwoD
{
    protected:
        string name, warpSpace;
        bool containsWarpSpace;
    public:
        //constructor
        ShapeTwoD();
        ShapeTwoD(string, bool);

        //accessors/set function
        void setName(string);

        //mutator/get function
        string getName();



        //methods
        virtual double computeArea();
        virtual void view();
};

shapetwod.cpp

ShapeTwoD::ShapeTwoD()
{
    string name = "";
}

ShapeTwoD::ShapeTwoD(string ShapeName)
{
    name = ShapeName;
}

void ShapeTwoD::setName(string shapeName)
{
    name=shapeName;
}

string ShapeTwoD::getName()
{
    return name;
}

double ShapeTwoD::computeArea()
{
    return 0;
}

void ShapeTwoD::view()
{
    cout << "Area is: " << endl;
}

square.h

class Square:public ShapeTwoD
{
    private:

        int xVal,yVal;
        int length, breath;
        double area;

    public:

        Square();
        Square(string, int, int, double);


        //acessor method
        //int getSquareDetails();
        int getxCord();
        int getyCord();
        double getArea();

        virtual double computeArea();
        void view();

        int xvalue[4];
        int yvalue[4];

};

square.cpp

Square::Square()
{
    xVal = 0;
    yVal = 0;
    area = 0;
}

Square::Square(string ShapeName, bool warpspace, int xval, int yval, double areas):ShapeTwoD(ShapeName, warpspace)
{
    xVal = xval;
    yVal = yval;
    area = areas;
}

void Square::setSquareCord()
{
    for (int i=0; i<4; i++)
    {
        cout << "Please enter x-ordinate of pt " << i+1 << ": "; 
        cin >> xVal;
        xvalue[i] = xVal;

        cout << endl;

        cout << "Please enter y-ordinate of pt " << i+1 << ": ";
        cin >> yVal;
        yvalue[i] = yVal;
        cout << endl;
    }
}

double Square::computeArea()
{
    int xmax = xvalue[1];
    int xmin = xvalue[1];

    int ymax = yvalue[1];
    int ymin = yvalue[1];

    for(int i=0; i<4; i++)
    {
        if(xvalue[i]>xmax)
        {
            xmax = xvalue[i];
        }
        else if(xvalue[i]<xmin)
        {
            xmin = xvalue[i];
        }
    }

    for(int i=0; i<4; i++)
    {
        if(yvalue[i]>ymax)
        {
            ymax = yvalue[i];
        }
        else if(yvalue[i]<ymin)
        {
            ymin = yvalue[i];
        }
    }
    length = xmax - xmin;
    breath = ymax - ymin;

    area = length * breath;
    return (area);
}

int Square::getxCord()
{
    return xVal;
}

int Square::getyCord()
{
    return yVal;
}

double Square::getArea()
{
    return area;
}


void Square::view()
{
    cout << "Name: " << getName() << endl;
    cout << "Area is: " << area << endl;
}

抱歉,我真的不知道如何分阶段提出我的问题。我实际上正在使用多态性& 这里的虚拟函数和我的 computeArea 和 view 函数都是虚拟的。

所以在我的“视图”函数下的 square.cpp 中,程序总是会返回 0,我不知道为什么会这样..

这就是我调用视图函数的方式。不确定它是否在这里有所帮助..

void Shape2DLink::InputSensor()
{
    string shape,type;  

    cout<<endl<<"\n"<<"[ Input sensor data ]"<<endl;


    cout << "Please enter name of shape: " << endl;
        cin >> shape;
        shape2D.setName(shape); 

    cout << "Please enter special type : " << endl;
        cin >> type;
        shape2D.setWarpSpace(type);

    if(shape == "Square")
    {
            square.setSquareCord();
            square.computeArea();
            square.isPointOnShape();
            square.isPointInShape();
            Square *mySquare = new Square;
            shapeobject.push_back(mySquare);
            //shapeobject.push_back( new Square );
    }
}
void Shape2DLink::Display()
{
    vector<ShapeTwoD*>::iterator vectorIt = shapeobject.begin();
    while(vectorIt != shapeobject.end())
    {
        (*vectorIt)->view();
        vectorIt++;
    }
}

【问题讨论】:

  • 我确信您包含的所有代码对您的问题都是必不可少的。
  • 我猜你没有使用指针,但你需要显示你在哪里/如何调用getAreacomputeArea
  • @DanielDaranas 抱歉,我只是想知道为什么我的正方形区域总是返回 0..
  • 您遗漏了最重要的代码,您实际上在其中创建、对象和调用其函数。
  • 也许您可以告诉我们您遇到的什么问题,而不是发布可能不相关的代码。如果我们需要查看更多代码,我们会向您索取。

标签: c++ constants virtual


【解决方案1】:

我只是想知道是否所有的虚函数都必须是一个常量?

没有。


我遇到了一些问题,因为我的区域总是返回 0 当我想把它们打印出来的时候。

嗯,好的,让我们看看。

double ShapeTwoD::computeArea()
{
    return 0;
}

是的,嗯。


我已经戴上了魔法猴帽,我预测您遇到的问题是由Object Slicing 引起的。考虑:

void foo (Shape2D shape)
{
  cout << shape.compute_area() << "\n"
}

int main()
{
  Square sq;
  foo (sq);
}

由于上面的foo 采用Shape2D 按值,因此传递给它的所有Squareness 都被切掉,只剩下一个Shape2D。当然,因为Shape2D::compute_area 只是返回0,所以这就是计算出来的。

要解决此特定问题,请不要复制对象或按值获取它,而是按引用

void foo (Shape2D& shape)
{
  cout << shape.compute_area() << "\n"
}

现在对象没有被切片。

在你没有给我们展示的代码中寻找类似的问题。

【讨论】:

    【解决方案2】:

    没有一个虚方法可能是也可能不是 const。您的问题在其他地方,但不在您包含的代码中。

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2013-03-09
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多