本文介绍了Const成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 据我所知,const对象使用const成员函数 确保其实例在其整个生命周期内不被修改。我是否错过了某些东西.. #include< iostream> 使用命名空间std; A级{ 公开: A(char& str,size_t sz){ pch = new char [sz]; strncpy(pch,& str,sz); } char * print()const { * pch =''M''; 返回pch; } ~A(){ 删除pch; } 私人: char * pch; }; int main(){ const char str [] =" ;我是一个常数; const A * obj = new A(* str,sizeof(str)); cout<<" obj - > print()="<< obj-> print()<< endl; 返回0; } Wg 解决方案 不,你做得对。下面代码中的A实例不是在''print''成员函数中修改的。成员''pch''修改了一些内存,即 ,但该内存不属于''A'' 对象的一部分。 您的程序在这里有未定义的行为。您应该使用 删除[] ,因为您使用''new的数组形式分配''pch'' ''。 它甚至可以编译吗?你的构造函数引用了非const char,你正在取消引用一个指向const char的指针... V - 请在通过电子邮件回复时删除资金''A' 我没有回复最热门的回复,请不要问 不,你做得对。下面代码中的A实例不是在''print''成员函数中修改的。成员''pch''修改了一些内存,即 ,但该内存不属于''A'' 对象的一部分。 您的程序在这里有未定义的行为。你应该使用 delete [] 如果我的理解是正确的,删除[]时会调用我们想要删除一个对象数组 - 需要为每个 对象调用析构函数。 不会如果我们使用是好的删除pch删除字符数组? 抱歉使用 - char str [] =" I a constant" ;; //而不是const char str [] =" I a constant" ;; 甚至可以编译吗?你的构造函数引用了非const char,你正在取消引用一个指向const char的指针... V - 请在回复时删除资金''A'电子邮件 我没有回复最热门的回复,请不要问 您正在修改pch指向的位置的CONTENTS而不是 the pch 例如pch指向内存中的1000位置你的代码是 修改1000处的内容而不是 $ b $中包含的值b''pch''(即1000)。 As I understand, a const member function is used by const object toensure that its instance isn''t modified throughout its life. Am Imissing something..#include <iostream>using namespace std;class A{public:A(char& str, size_t sz){pch = new char [sz];strncpy(pch, &str, sz);}char* print() const {*pch = ''M'';return pch;}~A(){delete pch;}private:char* pch;};int main(){const char str[] = "I a constant";const A* obj = new A(*str, sizeof(str));cout<<"obj->print() = "<<obj->print()<<endl;return 0;}Wg 解决方案No, you got it right. The instance of ''A'' in your code below is NOTmodified in the ''print'' member function. Some memory pointed to bythe member ''pch'' is modified, but that memory is not part of the ''A''object.Your program has undefined behaviour here. You''re supposed to usedelete[]since you allocate ''pch'' using the array form of ''new''.Does it even compile? Your constructor takes a reference to non-constchar, and you''re dereferencing a pointer to const char...V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t askNo, you got it right. The instance of ''A'' in your code below is NOTmodified in the ''print'' member function. Some memory pointed to bythe member ''pch'' is modified, but that memory is not part of the ''A''object.Your program has undefined behaviour here. You''re supposed to use delete[]If my understanding is correct, delete[] is called when we want todelete an array of objects - destructor needs to be called for eachobject.Won''t be Ok if we use " delete pch " for deleting char array ?Sorry use -char str[] = "I a constant"; // instead of const charstr[] = "I a constant";Does it even compile? Your constructor takes a reference to non-constchar, and you''re dereferencing a pointer to const char...V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t askyou are modifying the CONTENTS at the location pointed by pch and notthe pchfor example pch pointing to 1000 location in memory your code ismodifying the contents at 1000 not the value contained in by''pch''(i.e. 1000). 这篇关于Const成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!