Question 17: Which of the following identify const-correctness failures in the C++ program below?

template<typename T> class MyArray { public: MyArray(); MyArray(MyArray& copy); MyArray& operator=(MyArray& copy); //... }; class MyData { public: MyData(MyArray<int>& x, MyArray<int>& y); //... const MyArray<int>& x(); const MyArray<int>& y(); }; MyArray<int> read_data(int*, char**); void make_changes(MyData* edit); int main(int argc, char* argv[]) { const MyArray<int> read_x = read_data(&argc, argv); const MyArray<int> read_y = read_data(&argc, argv); MyData user_data(read_x, read_y); MyData edit_buffer(user_data); make_changes(&edit_buffer); }

 

 

    A. MyData(MyArray<int>& x, MyArray<int>& y); should be

MyData(const MyArray<int>& x, const MyArray<int>& y);

    B. MyArray& operator=(MyArray& copy); should be

const MyArray& operator=(const MyArray& copy);

    C. void make_changes(MyData* edit); should be

void make_changes(const MyData* edit);

    D. MyArray(MyArray& copy); should be

MyArray(const MyArray& copy);

    E. const MyArray& operator=(const MyArray& copy); should be

const MyArray& operator=(const MyArray& copy) const;

 

A

 

其他有几个也对 但是没有解决main中的编译错误

你可能感兴趣的:(C++,user,Class,buffer)