需要了解关于C++中Const函数的问题

3

关于我对这个问题的理解:

问题 #03:

  • 从您的课程项目中选择任何一个班级。"每个成员应该从他们的项目中选择不同的班级"。

  • 将其中一个int数据成员设为常量。

  • 创建四个函数以查看Constant关键字的使用情况。每个函数应返回修改后的整数(非void)。

函数名称 要修改的数据 函数类型
Int NonConstant( int x) 非常量 非常量
Int ConstantData( int x) 常量 非常量
Int NonFunction( int x) 非常量 常量
Int ConstantBoth( int x) 常量 常量

我并不是让别人替我完成这个问题,我只是想了解这个问题到底想要表达什么,因为如果我尝试按照图中显示的方式进行更改,我的代码会出现很多错误。

#include<iostream>
using namespace std;

class Project
{
    private:
        
        int const x;
        int y;
        
    public:
        
        Project():x(5){}

        int NonConstant(int const x){
            this->y=x;
            return y;
        }
        
        int ConstData(const int x)
            return x;
        }
//      
//      const int ConstFunction(int x){
//              x++;
//              return x;
//      }
//        const void ConstBoth(void) const {
//          x++;
//      }
};


int main()
{
    Project obj;
    cout<<"NonConstant(1): "<<obj.NonConstant(1)<<endl;
    cout<<"ConstData(1): "<<obj.ConstData(1)<<endl;
    cout<<"ConstFunction(1): "<<obj.ConstFunction(1)<<endl;
//  obj.ConstBoth();
}

2
"int const x" 和 "const int x" 是完全相同的。 - Some programmer dude
11
写那个问题的人在写作方面本可以做得更好。 - Jason
1
此外,返回一个const值没有实际意义。调用者仍然可以将返回值分配给非const变量。而返回const void甚至更少意义,因为根本没有返回任何东西。 - Some programmer dude
1
@AnoopRana 当然没问题。 - mrc
类 Project { private: const int x; int y; public: Project():x(5){} int NonConstant(int x){ return x; } int ConstData(const int x){ return x; } const int ConstFunction(int x){ return x; } const int ConstBoth(const int x){ return x; } };int main() { Project obj; cout<<"NonConstant(1): "<<obj.NonConstant(1)<<endl; cout<<"ConstData(1): "<<obj.ConstData(1)<<endl; cout<<"ConstFunction(1): "<<obj.ConstFunction(1)<<endl; cout<<"ConstBoth(1): "<<obj.ConstBoth(1); } - mrc
显示剩余6条评论
2个回答

0

这是你想要的吗?
我不得不重写你的最后一个函数,因为它违反了常量函数的原则。

编辑:根据问题#3修改了函数。

#include<iostream>
using namespace std;

class Project
{
private:

    int const x;
    int y;

public:

    Project():x(5) {}

    // Non-constant data & function
    int NonConstant(int a)
    {
        this->y = a;
        return y;
    }

    // Constant data & Non-constant function
    int ConstData(const int a)
    {
        //a++; // You cannot modify const variables
        return a;
    }

    // Non-constant data & Constant function
    int ConstFunction(int a) const
    {
        a++;
        return a;
    }

    int ConstBoth(const int a) const
    {
        //a++; // You cannot modify const variables
        return a;
    }
};


int main()
{
    Project obj;
    cout<<"NonConstant(1): "<<obj.NonConstant(1)<<endl;
    cout<<"ConstData(1): "<<obj.ConstData(1)<<endl;
    cout<<"ConstFunction(1): "<<obj.ConstFunction(1)<<endl;
    cout<<"ConstBoth(1): "<<obj.ConstBoth(1)<<endl;
}

2
希望这不是 OP 需要的,因为 const int ConstFunction(int x) 在很多方面都是无意义的。 - Captain Giraffe
@CaptainGiraffe 我同意在所有情况下返回值应该只是int,但似乎他/她只是想尝试一下。 - Adrian
我想ConstFunction应该是int ConstFunction(int x) const {,表示一个不修改当前对象或其字段的函数。 - Rogue

0
假设有一个结构体,它有两个成员c和x
struct S{
    // This is already quite unusual.
    // Nobody can change c after creation.
    // Because of constness.
       const int c = 42;   // Good useful number

    // x is part of the objects state.
    // x is important to protect for some reason or other.
    // Lets make it private:
    private:
       int x; 
    public:
    // Now how can others access x. 
    // In a non const way
    int& get_x_non();
    // Or in a const way 1 (A copy of out const):
    int get_x() const;
    // Or in a const way 2 (Our actual x as const, don't use with 1):
    const int& get_x() const;
    // The const after the function is part of the functions cv-qualifiers.
    // And makes a promise to the caller user that the object
    // doesn't change if get_x() is called.
    
    // So lets try to make a member function change c.
    void set_c(int new_c){
        // The compiler will not let this happen.
        c = new_c; 
    }
}

所以:

void f(){
    S s;
    s.get_x_non() = 43; // Ok!
    s.get_x() = 44 // Can't happen
    int i = s.get_x(); // Always works.
    int j = s.c;  // Always works
    // neither i or j is const.
}

你可能已经注意到,我无法将这些典型的const场景与你的作业问题联系起来,这就是为什么@Anoop的评论得到了很多欢呼声的原因。

因此,我们将会在帮助你解决这个特定问题上遇到困难。你需要向任何可用的教学人员提出问题。

你可以在这里阅读更多信息:const成员函数和非const成员函数之间有什么区别?


@RemyLebeau 谢谢,这是一个改进。 - Captain Giraffe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接