Const成员函数 [英] Const member function

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

问题描述

据我所知,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

解决方案

Wolfgang写道:


据我所知,const成员函数就是我们由const对象编辑为

确保其实例在其整个生命周期内不被修改。我是否b $ b缺少一些东西..



不,你做得对。下面代码中的A实例不是在''print''成员函数中修改的
。成员''pch''修改了一些内存,即

,但该内存不属于''A''

对象的一部分。


#include< iostream>

使用命名空间std;


A类{
< br $>
public:


A(char& str,size_t sz){

pch = new char [sz];

strncpy(pch,& str,sz);

}


char * print()const {

* pch =''M'';

返回pch;

}

~A(){

delete pch;



您的程序在这里有未定义的行为。您应该使用


删除[]


,因为您使用''new的数组形式分配''pch'' ''。


}


私人:

char * pch;

};


int main(){


const char str [] =" I a constant";


const A * obj = new A(* str,sizeof(str));



它甚至可以编译吗?你的构造函数引用了非const

char,你正在取消引用一个指向const char的指针...


cout< ;<" obj-> print()="<< obj-> print()<< endl;


返回0;

}


Wg



V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


6月15日下午5:54,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:


Wolfgang写道:


我理解,const对象使用const成员函数

确保其实例在其整个生命周期内不被修改。我是否b $ b缺少一些东西..



不,你做得对。下面代码中的A实例不是在''print''成员函数中修改的
。成员''pch''修改了一些内存,即

,但该内存不属于''A''

对象的一部分。


#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'';

返回pch;

}

~A(){

删除pch;



您的程序在这里有未定义的行为。你应该使用


delete []



如果我的理解是正确的,删除​​[]时会调用我们想要
删除一个对象数组 - 需要为每个

对象调用析构函数。


不会如果我们使用是好的删除pch删除字符数组?


>

因为你使用''new''的数组形式分配''pch''。


}


private:

char * pch ;

};


int main(){


const char str [] =我是一个常数;



抱歉使用 -

char str [] =" I a constant" ;; //而不是const char

str [] =" I a constant" ;;


>


const A * obj = new A(* str,sizeof(str));



甚至可以编译吗?你的构造函数引用了非const

char,你正在取消引用一个指向const char的指针...


cout< ;<" obj-> print()="<< obj-> print()<< endl;


返回0;

}


Wg



V

-

请在回复时删除资金''A'电子邮件

我没有回复最热门的回复,请不要问


仔细看看是什么你在做什么

char * print()const {


* pch =''M'';

返回pch;

}



您正在修改pch指向的位置的CONTENTS而不是

the pch

例如pch指向内存中的1000位置你的代码是

修改1000处的内容而不是
$ b $中包含的值b''pch''(即1000)。


As I understand, a const member function is used by const object to
ensure that its instance isn''t modified throughout its life. Am I
missing 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

解决方案

Wolfgang wrote:

As I understand, a const member function is used by const object to
ensure that its instance isn''t modified throughout its life. Am I
missing something..

No, you got it right. The instance of ''A'' in your code below is NOT
modified in the ''print'' member function. Some memory pointed to by
the member ''pch'' is modified, but that memory is not part of the ''A''
object.

#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;

Your program has undefined behaviour here. You''re supposed to use

delete[]

since you allocate ''pch'' using the array form of ''new''.

}

private:
char* pch;
};

int main(){

const char str[] = "I a constant";

const A* obj = new A(*str, sizeof(str));

Does it even compile? Your constructor takes a reference to non-const
char, and you''re dereferencing a pointer to const char...

cout<<"obj->print() = "<<obj->print()<<endl;

return 0;
}

Wg

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Jun 15, 5:54 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Wolfgang wrote:

As I understand, a const member function is used by const object to
ensure that its instance isn''t modified throughout its life. Am I
missing something..


No, you got it right. The instance of ''A'' in your code below is NOT
modified in the ''print'' member function. Some memory pointed to by
the member ''pch'' is modified, but that memory is not part of the ''A''
object.

#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;


Your program has undefined behaviour here. You''re supposed to use

delete[]

If my understanding is correct, delete[] is called when we want to
delete an array of objects - destructor needs to be called for each
object.

Won''t be Ok if we use " delete pch " for deleting char array ?

>
since you allocate ''pch'' using the array form of ''new''.

}

private:
char* pch;
};

int main(){

const char str[] = "I a constant";

Sorry use -
char str[] = "I a constant"; // instead of const char
str[] = "I a constant";

>

const A* obj = new A(*str, sizeof(str));


Does it even compile? Your constructor takes a reference to non-const
char, and you''re dereferencing a pointer to const char...

cout<<"obj->print() = "<<obj->print()<<endl;

return 0;
}

Wg


V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Well see carefully what you''re doing
char* print() const {

*pch = ''M'';
return pch;
}

you are modifying the CONTENTS at the location pointed by pch and not
the pch
for example pch pointing to 1000 location in memory your code is
modifying the contents at 1000 not the value contained in by
''pch''(i.e. 1000).


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

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