d****n 发帖数: 130 | 1 1. 一般如果函数的参数是引用或指针类型,而且在函数中不会改变,一般都应该声明
成const,对不对?
2. 那么member function什么时候应该声明成const呢?如果函数不修改类的成员,是
不是也尽量应该声明成const,我的理由是,如果第一条成立,那么类就会经常有const
的object,如果member function不声明成const,就没法调用这个member funciton.
关于const有什么使用的guideline吗? |
f*****Q 发帖数: 1912 | 2 http://duramecho.com/ComputerInformation/WhyHowCppConst.html
const
【在 d****n 的大作中提到】 : 1. 一般如果函数的参数是引用或指针类型,而且在函数中不会改变,一般都应该声明 : 成const,对不对? : 2. 那么member function什么时候应该声明成const呢?如果函数不修改类的成员,是 : 不是也尽量应该声明成const,我的理由是,如果第一条成立,那么类就会经常有const : 的object,如果member function不声明成const,就没法调用这个member funciton. : 关于const有什么使用的guideline吗?
|
d****n 发帖数: 130 | 3 多谢了,很有用。
还有一个问题,从一个函数返回一const指针的意义是什么?
比如const int *fun(void);
为什么要const呢?返回的内容为什么不应改变?
【在 f*****Q 的大作中提到】 : http://duramecho.com/ComputerInformation/WhyHowCppConst.html : : const
|
X****r 发帖数: 3557 | 4 const int * 是一个指向const int的指针,不是一个指向int的const指针,后者是int
* const
【在 d****n 的大作中提到】 : 多谢了,很有用。 : 还有一个问题,从一个函数返回一const指针的意义是什么? : 比如const int *fun(void); : 为什么要const呢?返回的内容为什么不应改变?
|
r*********r 发帖数: 3195 | 5 比如 const char *what() { return "bad"; } . 这里必须返回 const char *. |
d****n 发帖数: 130 | 6 对,我的语言不严谨。那么这函数返回指向const int的指针的意义是什么?
int
【在 X****r 的大作中提到】 : const int * 是一个指向const int的指针,不是一个指向int的const指针,后者是int : * const
|
d****n 发帖数: 130 | 7 那如果不是const char 呢?
【在 r*********r 的大作中提到】 : 比如 const char *what() { return "bad"; } . 这里必须返回 const char *.
|
E*****7 发帖数: 128 | 8 const int *fun(void)
{
const int x, y, z, volume ;
x=1; y = 2, z=3 ;
volume = x * y * z ;
return &volume ;
}
I do not know if this makes sense or not. Here we do not want the volume
calculated from fun() function to be modified outside of fun() after it is
returned. |
d****n 发帖数: 130 | 9 volume已经被释放了,不能传回。
【在 E*****7 的大作中提到】 : const int *fun(void) : { : const int x, y, z, volume ; : x=1; y = 2, z=3 ; : volume = x * y * z ; : return &volume ; : } : I do not know if this makes sense or not. Here we do not want the volume : calculated from fun() function to be modified outside of fun() after it is : returned.
|
S*********g 发帖数: 5298 | 10 1. your code won't compile
2. you returned a local pointer
【在 E*****7 的大作中提到】 : const int *fun(void) : { : const int x, y, z, volume ; : x=1; y = 2, z=3 ; : volume = x * y * z ; : return &volume ; : } : I do not know if this makes sense or not. Here we do not want the volume : calculated from fun() function to be modified outside of fun() after it is : returned.
|
|
|
r*********r 发帖数: 3195 | 11 this is so completely wrong. you should return by value here.
【在 E*****7 的大作中提到】 : const int *fun(void) : { : const int x, y, z, volume ; : x=1; y = 2, z=3 ; : volume = x * y * z ; : return &volume ; : } : I do not know if this makes sense or not. Here we do not want the volume : calculated from fun() function to be modified outside of fun() after it is : returned.
|
E*****7 发帖数: 128 | 12
So run the following code, it compiles on my machine:
const int *fun(void)
{
const int x=1, y = 2 , z=3 ;
const int volume = x * y * z ;
return &volume ;
}
int main()
{
const int* pt = fun() ;
cout << "*pt=" << *pt << endl;
return 0;
}
// output: *pt=6
【在 S*********g 的大作中提到】 : 1. your code won't compile : 2. you returned a local pointer
|
S*********g 发帖数: 5298 | 13 This can compile.
However, this is still a wrong approach since it returns a pointer to a
local object. When the code exits from fun(), int volume is destroyed.
Therefore, you returned a dangling pointer.
【在 E*****7 的大作中提到】 : : So run the following code, it compiles on my machine: : const int *fun(void) : { : const int x=1, y = 2 , z=3 ; : const int volume = x * y * z ; : return &volume ; : } : int main() : {
|
r*********r 发帖数: 3195 | 14 c/c++ allows you to shot yourself in the foot, and you just did.
fortunately, you missed. |
E*****7 发帖数: 128 | 15 Thanks, you are correct! I am learning C++. Let me try the followings:
const int *fun(void)
{
const int x=1, y = 2 , z=3 ;
static const int volume = x * y * z ;
return &volume ;
}
int main()
{
const int* pt = fun() ;
cout << "*pt=" << *pt << endl;
}
Is it correct now? |
d****n 发帖数: 130 | 16 这个理论上没大问题了,但INTERVIEW的时候最好不要这么写。
现在大家能不能回来回答一下我原来的问题,为什么用const? 仅仅因为在函数中是
const int吗?好象还有其它原因。
【在 E*****7 的大作中提到】 : Thanks, you are correct! I am learning C++. Let me try the followings: : const int *fun(void) : { : const int x=1, y = 2 , z=3 ; : static const int volume = x * y * z ; : return &volume ; : } : int main() : { : const int* pt = fun() ;
|
p****s 发帖数: 32405 | 17 你不要用static混过去, 老老实实做个dynamic alloc mem不好么
【在 E*****7 的大作中提到】 : Thanks, you are correct! I am learning C++. Let me try the followings: : const int *fun(void) : { : const int x=1, y = 2 , z=3 ; : static const int volume = x * y * z ; : return &volume ; : } : int main() : { : const int* pt = fun() ;
|
S*********g 发帖数: 5298 | 18 点点点。。。
这种情况下应该直接返回value
【在 p****s 的大作中提到】 : 你不要用static混过去, 老老实实做个dynamic alloc mem不好么
|
E*****7 发帖数: 128 | 19
"INTERVIEW的时候最好不要这么写。" 为什么不?
"为什么用const? " 用const是为了(1)对CLASS而言,保护OBJECT的DATA MEMBER的状
态不能被随意改动,要改动必须使用const函数。(2)对变量而言,一旦付值,不能再改动。
“仅仅因为在函数中是
【在 d****n 的大作中提到】 : 这个理论上没大问题了,但INTERVIEW的时候最好不要这么写。 : 现在大家能不能回来回答一下我原来的问题,为什么用const? 仅仅因为在函数中是 : const int吗?好象还有其它原因。
|
E*****7 发帖数: 128 | 20
LZ问const int *fun(void),你却建议const int fun(void)。答非所问了吧?
【在 S*********g 的大作中提到】 : 点点点。。。 : 这种情况下应该直接返回value
|
f*****Q 发帖数: 1912 | 21 俺是土人,没看出来有什么实际意义。也许传回一个数组又不让人家改?
例如这个
#include
int intArray[] = {1, 2, 3};
const int * fun(){
return intArray;
}
int main (int argc, char * const argv[]) {
const int * a = fun();
//a[ 1 ]=0; !!error!!
std::cout<
return 0;
}
【在 d****n 的大作中提到】 : 多谢了,很有用。 : 还有一个问题,从一个函数返回一const指针的意义是什么? : 比如const int *fun(void); : 为什么要const呢?返回的内容为什么不应改变?
|
c*******3 发帖数: 21 | 22
The first answer is why not. Because if you assign the return value to some
other variable a=f(); it does not
hurt if f() return a const (just like you can use a=5).
A further point is that returning const can avoid things like: f() = 1.
Although one may never want to do
this intentionally, it may happen if one has a typo in if (f() == 1).
Returning a const can let the compiler
find that typo.
【在 d****n 的大作中提到】 : 对,我的语言不严谨。那么这函数返回指向const int的指针的意义是什么? : : int
|