首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

CString 是否不可以作为返回值,请大家看一看

2013-09-28 
CString 是不是不可以作为返回值,请大家看一看有这么两个函数:CString f1()void f2(const cahr* p)然后

CString 是不是不可以作为返回值,请大家看一看
有这么两个函数:

CString f1();
void f2(const cahr* p);

然后我这样用: 
f2(f1());

这样是不是会有问题?在f2内部这个临时CString是不是已经销毁了呢?我想知道标准是怎么规定的,我强烈怀疑vc2008就是这么干的
[解决办法]
没有问题。。
[解决办法]
http://stackoverflow.com/questions/584824/guaranteed-lifetime-of-temporary-in-c
[解决办法]
好好看看帖子,哪里不懂就问我,不要太着急。

你前后举这两个例子有着本质的区别。
前者,f1的返回值,在f2中被使用。这种情况下,在f2没有返回之前,f1的返回值都是有效的。
后者,在这句代码
const char* psz = f();
执行完成后就释放掉了,所以你后面再想用f()的返回值就可能会出错了。
[解决办法]
原因先不说,结论是CString可以当做返回值。
[解决办法]
引用:
原因先不说,结论是CString可以当做返回值。

他的问题是,这个返回值在他用的时候会不会已经释放了。
[解决办法]
首先 Cstring 可以作为返回值  其次 楼主直接把Cstring放到了本来应该char*作为参数的参数位置 最好还是强转一下 (LPSTR)(LPCTSTR)cstring 
最后 返回值在使用的时候释放的是你的临时变量吧 
[解决办法]
可以作为返回值,没有问题!
[解决办法]
lz的这种问题其实就是问的临时变量的行作用域问题。
变量或类型的作用域就这几种:
静态作用域、块作用域和行作用域
行作用域中产生的所有临时对象都在离开该行时才可能*发生销毁。当然不要把行之中的函数内部的临时变量也算进去。
“可能”的具体含义是 如果外部有一个块作用域的常引用或右值引用直接引用到该对象。否则在离开行的时候就发生销毁
A func1(void);
B func2(const A &);


  func2(func1());//行作用域。A和B都将在离开行时才销毁。在func2的作用域内部A一直有效。而B形式上将在func2 的return期间构造而在;这个点上析构。


[解决办法]
引用:
Quote: 引用:

原因先不说,结论是CString可以当做返回值。

他的问题是,这个返回值在他用的时候会不会已经释放了。

不会,在临时变量会在这一行f2(f1());代码执行完之后才释放。
[解决办法]
问题很简单,CString是一个类,你在函数中返回CString,而这个CString如果是你在函数内部声明的,则很可能当函数结束时这个类的实例已经没了
[解决办法]
行作用域就是一整行。多少层的调用都一样。这个如果不是确定的话,右值引用什么的都不会大行其道了。

#include <iostream>
#include <conio.h>


class A1
{
public:
A1(int) { std::cout << "construct A1" << std::endl; };
~A1() { std::cout << "destruct A1" << std::endl; };
};

class A2
{
public:
A2(int) { std::cout << "construct A2" << std::endl; };
~A2() { std::cout << "destruct A2" << std::endl; };
};

class A3
{
public:
A3(int) { std::cout << "construct A3" << std::endl; };
~A3() { std::cout << "destruct A3" << std::endl; };
};

class A4
{
public:
A4(int) { std::cout << "construct A4" << std::endl; };
~A4() { std::cout << "destruct A4" << std::endl; };
};

A1 func1(void)
{
std::cout << "func1() called." << std::endl;
return(0);
};

A2 func2(const A1 &)
{
std::cout << "func2() called." << std::endl;
return(0);
};

A3 func3(const A2 &)
{
std::cout << "func3() called." << std::endl;
return(0);
};

A4 func4(const A3 &)
{
std::cout << "func4() called." << std::endl;
return(0);
};

int main(void)
{
{
std::cout << "-----------------start calls *1." << std::endl;
func4(func3(func2(func1())));
std::cout << "-----------------end calls *1." << std::endl;
}
{
std::cout << "-----------------start calls *2." << std::endl;
auto const & q = func4(func3(func2(func1())));
std::cout << "-----------------end calls *2." << std::endl;
}
_getch();
return 0;
}

执行结果:

-----------------start calls *1.
func1() called.
construct A1
func2() called.
construct A2
func3() called.


construct A3
func4() called.
construct A4
destruct A4
destruct A3
destruct A2
destruct A1
-----------------end calls *1.
-----------------start calls *2.
func1() called.
construct A1
func2() called.
construct A2
func3() called.
construct A3
func4() called.
construct A4
destruct A3
destruct A2
destruct A1
-----------------end calls *2.
destruct A4

热点排行