使用const成员函数 [英] Using a const member function

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

问题描述

我的朋友和我正在研究一个叫做矩形的类。

我们有一个成员函数返回一个正方形(我们班级的对象)

等价面积平均面积为2平方。

so(方形1的面积+方形2的面积)/ 2

。返回方形的边必须四舍五入到最接近的方。我们正在研究这个问题,经过调试,我们意识到函数末尾的const会阻止我们改变值。

我希望我提供了足够的信息。

我的问题是,如果我们无法修改此函数中的值,我们的方法应该是什么?请注意,我们不是简单地创建新函数来编写类的实现。还有其他功能,但我只是展示这些,因为它们与我的问题最相关,其余的似乎没问题。



  class  square 
{
public
square( int s);
int plus(square sq) const ;
square avgSquare(square sq) const ; // 这是我们正在尝试修复的功能
~quare(); // 默认构造函数

私人
int 方面;
};

// ___________以下_________课程实施

< span class =code-comment> // 参数化构造函数,默认参数

square :: square( int s = 0
{
side = s;
}

// 下面的函数似乎很好(区域是另一个函数,工作)

int square :: plus(square sq) const
{
int plus = sq.area()+ sq.area();
int area1 = sq.area();
int area2 = sq.area();
plus = area1 + area2;
返回加;
}

// 好的人以下是我们正在尝试的功能修复。
// 我写过我们如何填写你尝试了什么 section

square square :: avgSquare(square sq) const
{
}





我的尝试:



我们首先编码如何获得平均值。并且存在一些分歧。我将展示我们认为应该做的两种方式。但是我们没有意识到const给我们的限制



 square square :: avgSquare(square sq) const  
{
// way one
double Asq = round(加(sq)/ 2);
return Asq;
// 第二种方式
double Asq = round(sq.plus()/ 2);
return Asq;

return sq.plus()/ 2;
}

解决方案

你试过编译吗?第二种方法无法编译,因为你没有通过论证。



有几种不同的方法可以实现avgSquare。这是作为静态方法的一种方式。



 静态  double  square :: avgSquare(square& s1,square& s2)
{
int areasum = s1.area()+ s2.area();
double asq = areasum / 2 0 ;
return asq;
}


如果你可以创建一个const函数,你应该这样做。你也应该通过const引用传递非基本类型的参数,如果它们没有被修改而不是按值传递它们。



但你的例子有问题:

函数 avgSquare 被定义为返回 square 但是你的实现返回 double int



可能的解决方案可能是:

  //  此功能的实现未显示 
int area() const ;

int avgSquare( const square& s1, const square& s2) const
{
return (s1.area()+ s2.area())/ 2 ;
}

// 可选的附加实现
int avgSquare( const square& other) const
{
return avgSquare(* this ,other);
}

如果您需要返回 double 除以2.0:

 double avgSquare(const square& s1,const square& s2)const 
{
return(s1.area()+ s2.area())/ 2.0;
}



只有几个简单的操作,因此不需要使用其他成员函数,如 plus()


My friend and I are working on a class called rectangle.
We have a member function that returns a square (object of our class)
of an area equivalent to the average area of 2 squares.
so (area of square 1 + area of square 2)/ 2
.the side of the returned square must be rounded to the nearest ones. We were working on this and upon debugging we realized that const at the end of our function prevents us from altering values.
I hope I have provided enough information.
My question is what should our approach be if we cannot modify values inside this function? please note that we are not to create new functions simply code the implementation of the class. Also there are other functions but I'm only displaying these because they pertain most to my question and the rest seem to be fine.

class square
{
public: 
    square(int s);
    int plus(square sq) const;
    square avgSquare(square sq) const; // this the function we are trying to fix
    ~square(); // default constructor
 
private:
    int side;
};
 
//_________class implementation below___________

// parameterized constructor with default parameter 

square::square(int s=0) 
{
    side = s;
}

// the function below seems fine( area is another function that works)

int square::plus(square sq) const
{
    int plus = sq.area() + sq.area();
    int area1 = sq.area();
    int area2 = sq.area();
    plus = area1 + area2;
    return plus;
}
 
//ok folks so below is the function we are trying to fix.
//I have written how we filled it in the "what have you tried" section

square square::avgSquare(square sq) const
{
}



What I have tried:

we first coded how to get the average. And there is a bit of disagreement. I will show the two ways we think this should be done. But we were unaware about the restrictions placed on us by const

square square::avgSquare(square sq) const
{
    //way one
    double Asq = round(plus(sq)/2);
    return Asq;
    //way two
    double Asq = round(sq.plus()/2);
    return Asq;
  
    return sq.plus()/2;
}

解决方案

Have you tried to compile this? Way two will not compile because you did not pass it an argument.

There are several different ways avgSquare could be implemented. Here is one way as a static method.

static double square::avgSquare( square & s1, square & s2 )
{
    int areasum = s1.area() + s2.area();
    double asq = areasum / 2.0;
    return asq;
}


If you can make a function const, you should do it. You should also pass parameters that are not basic types by const reference if they are not modified instead of passing them by value.

But there is a problem with your example:
The function avgSquare is defined as returning a square but your implementation returns a double or an int.

A possible solution might be:

// Implementation for this function not shown
int area() const;

int avgSquare(const square& s1, const square& s2) const
{
    return (s1.area() + s2.area()) / 2;
}

// An optional additional implementation
int avgSquare(const square& other) const
{
    return avgSquare(*this, other);
}

If you need to return a double just divide by 2.0:

double avgSquare(const square& s1, const square& s2) const
{
    return (s1.area() + s2.area()) / 2.0;
}


There are only a few simple operations so that there is no need to use other member functions like plus().


这篇关于使用const成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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