注册 登录
编程论坛 C++教室

1.非常对象用什么方式调用常函数(非常函数存在的情况下)2.在Show函数用++radius,为什么getAeas函数值发生变化。

winnie96 发布于 2016-03-22 13:52, 3052 次点击
#include <iostream>
using namespace std;

class Circle
{
    private:
        double radius;
        const double PI;
    public:
        Circle(double n):radius(n),PI(3.1415925){
        }        
        void Set(double n){
            radius=n;
        }
        double getArea()const
        {
            return PI*radius*radius;
        }
        void Show();
        void Show()const;
};

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void Circle::Show()
{
    cout<<getArea()<<"非常成员函数"<<++radius<<endl;
     
}
void Circle::Show ()const
{
    cout<<getArea()<<"常成员函数"<<radius<<endl;
}
int main(int argc, char *argv[]) {
   
    Circle c1(2);
    c1.Show();
    Circle const c2(2);
    c2.Show();
    return 0;
}
6 回复
#2
rjsp2016-03-22 14:34
1.非常对象用什么方式调用常函数(非常函数存在的情况下)
((const Circle&)c1).Show();

2.在Show函数用++radius,为什么getAeas函数值发生变化。
cout<<getArea()<<"非常成员函数"<<++radius<<endl; 先执行getArea()还是++radius属于“未指定行为”
#3
winnie962016-03-22 14:57
回复 2楼 rjsp
那怎样指定行为??
#4
rjsp2016-03-22 14:59
以下是引用winnie96在2016-3-22 14:57:21的发言:

那怎样指定行为??
听不懂你在问什么,请讲清晰完整的话
#5
winnie962016-03-22 15:03
以下是引用rjsp在2016-3-22 14:34:27的发言:

1.非常对象用什么方式调用常函数(非常函数存在的情况下)
((const Circle&)c1).Show();

[quote]2.在Show函数用++radius,为什么getAeas函数值发生变化。
cout<<getArea()<<"非常成员函数"<<++radius<<endl; 先执行getArea()还是++radius属于“未指定行为

[/quote]
#6
winnie962016-03-22 15:06
回复 4楼 rjsp
怎样才能使它先执行gerArea函数最后radius自增1.?
#7
rjsp2016-03-22 15:19
怎样才能使它先执行gerArea函数最后radius自增1.?
cout<<getArea();
cout<<++radius;

“未指定行为”就是C/C++标准中的 unspecified-behavior
想让某行为不再是未指定行为,除非你能让C++标准委员会修改C++标准。
1