CRITICAL SKILL 9.4: Returning Objects 对象的返回
2011-10-18 17:02
134 查看
CRITICAL SKILL9.4: Returning Objects 对象的返回
Just as objects can be passed to functions, functionscan return objects. To return an object, first declare the function asreturning a class type. Second, return an object of that type using the normalreturn statement. The
following program has a member function called mkBigger(). It returns an object that gives val a value twice as large as the invokingobject.
>>>>>>就像对象可以传递给函数一样,函数也可以返回对象。要返回一个对象,首先声明的函数的返回类型是类的类型。其次,使用通常的返回语句返回那种类型的对象。以下的程序有一个成员函数叫做mkBigger()。他返回一个对象,这个对象给出的val值时调用她的对象的两倍。
#include <iostream>
using namespace std;
class MyClass{
intval;
public:
//normalconstructor.
MyClass(inti){
val = i;
cout<<"inside constructor\n";
}
~MyClass(){
cout<<"destructing\n";
}
intgetval(){return val;}
//return anobject. mkBigger returns a MyClass object.
MyClass mkBigger(){
MyClass obj(val*2);
returnobj;
}
};
void display(MyClass ob)
{
cout<<ob.getval()<<"\n";
}
int main()
{
cout<<"beforeconstructing a.\n";
MyClass a(10);
cout<<"after constructing a.\n";
cout<<"beforecall to display().\n";
display(a);
cout<<"after display() return.\n\n ";
cout<<"beforecall to mkBigger().\n";
a = a.mkBigger();
cout<<"after mkBigger() return.\n\n";
cout<<"beforesecond call to display().\n";
display(a);
cout<<"afterdisplay() returns .\n\n";
return0;
}
Output:
![](http://hi.csdn.net/attachment/201110/18/0_1318928670kfL6.gif)
In this example, mkBigger( )creates a local object called o that has a val value twice that of the invokingobject. This object is then returned by the function and assigned to a insidemain( ). Then o is destroyed, causing
the first “Destructing” message to bedisplayed. But what explains the second call to the destructor?
When an object is returned by afunction, a temporary object is automatically created, which holds the returnvalue. It is this object that is actually returned by the function. After thevalue has been returned, this object
is destroyed. This is why the output showsa second “Destructing” message just before the message “After mkBigger( )returns.” This is the temporary object being destroyed.
As was the case when passing anobject to a function, there is a potential problem when returning an objectfrom a function. The destruction of this temporary object may cause unexpectedside effects in some situations. For example,
if the object returned by thefunction has a destructor that releases a resource (such as memory or a filehandle), that resource will be freed even though the object that is assignedthe return value is still using it. The solution to this type of probleminvolves
the use of a copy constructor, which is described next.
One last point: It is possible fora function to return an object by reference, but you need to be careful thatthe object being referenced does not go out of scope when the function isterminated.
>>>>>>在这个例子中,mkBigger()函数创建一个局部对象叫做obj,val的值是调用它的那个对象的两倍。Obj对象由函数返回,然后在main()函数里面赋值。然后obj销毁,造成第一个“destructing”消息显示。但是第二个怎么解释?
当函数返回一个对象的时候,一个临时的对象会被自动的创建,用来保存返回的值。由函数返回的事实上就是这个临时的对象。在值返回之后,这个临时的对象就会销毁。这就是为什么会在“After mkBigger( ) returns.”之前输出第二个“destructing”,这是临时对象正在被销毁。
就像我们之前看到给函数传递对象,当从函数返回一个对象的时候也有一个潜在的问题。临时对象的销毁在某些情况下可能带有副作用。如,如果函数返回对象的析构(销毁)函数释放了资源(如内存或者文件句柄),这些资源会被释放,即使被赋了值对象还有使用它。这种问题的解决方案会涉及到使用拷贝构造函数,接下来会讨论。
最后一点,使用引用返回一个函数的对象也是可行的,但是要注意,在函数结束的时候被引用的对象不能够跑出作用域。
>>>>>>>>>>>>>>>>>>>>>>>translatedby ouyangjun
>>>>>>>>>>>>>>>>>>>>>>>e-mail:ouyangjun1985#msn.com
Just as objects can be passed to functions, functionscan return objects. To return an object, first declare the function asreturning a class type. Second, return an object of that type using the normalreturn statement. The
following program has a member function called mkBigger(). It returns an object that gives val a value twice as large as the invokingobject.
>>>>>>就像对象可以传递给函数一样,函数也可以返回对象。要返回一个对象,首先声明的函数的返回类型是类的类型。其次,使用通常的返回语句返回那种类型的对象。以下的程序有一个成员函数叫做mkBigger()。他返回一个对象,这个对象给出的val值时调用她的对象的两倍。
#include <iostream>
using namespace std;
class MyClass{
intval;
public:
//normalconstructor.
MyClass(inti){
val = i;
cout<<"inside constructor\n";
}
~MyClass(){
cout<<"destructing\n";
}
intgetval(){return val;}
//return anobject. mkBigger returns a MyClass object.
MyClass mkBigger(){
MyClass obj(val*2);
returnobj;
}
};
void display(MyClass ob)
{
cout<<ob.getval()<<"\n";
}
int main()
{
cout<<"beforeconstructing a.\n";
MyClass a(10);
cout<<"after constructing a.\n";
cout<<"beforecall to display().\n";
display(a);
cout<<"after display() return.\n\n ";
cout<<"beforecall to mkBigger().\n";
a = a.mkBigger();
cout<<"after mkBigger() return.\n\n";
cout<<"beforesecond call to display().\n";
display(a);
cout<<"afterdisplay() returns .\n\n";
return0;
}
Output:
![](http://hi.csdn.net/attachment/201110/18/0_1318928670kfL6.gif)
In this example, mkBigger( )creates a local object called o that has a val value twice that of the invokingobject. This object is then returned by the function and assigned to a insidemain( ). Then o is destroyed, causing
the first “Destructing” message to bedisplayed. But what explains the second call to the destructor?
When an object is returned by afunction, a temporary object is automatically created, which holds the returnvalue. It is this object that is actually returned by the function. After thevalue has been returned, this object
is destroyed. This is why the output showsa second “Destructing” message just before the message “After mkBigger( )returns.” This is the temporary object being destroyed.
As was the case when passing anobject to a function, there is a potential problem when returning an objectfrom a function. The destruction of this temporary object may cause unexpectedside effects in some situations. For example,
if the object returned by thefunction has a destructor that releases a resource (such as memory or a filehandle), that resource will be freed even though the object that is assignedthe return value is still using it. The solution to this type of probleminvolves
the use of a copy constructor, which is described next.
One last point: It is possible fora function to return an object by reference, but you need to be careful thatthe object being referenced does not go out of scope when the function isterminated.
>>>>>>在这个例子中,mkBigger()函数创建一个局部对象叫做obj,val的值是调用它的那个对象的两倍。Obj对象由函数返回,然后在main()函数里面赋值。然后obj销毁,造成第一个“destructing”消息显示。但是第二个怎么解释?
当函数返回一个对象的时候,一个临时的对象会被自动的创建,用来保存返回的值。由函数返回的事实上就是这个临时的对象。在值返回之后,这个临时的对象就会销毁。这就是为什么会在“After mkBigger( ) returns.”之前输出第二个“destructing”,这是临时对象正在被销毁。
就像我们之前看到给函数传递对象,当从函数返回一个对象的时候也有一个潜在的问题。临时对象的销毁在某些情况下可能带有副作用。如,如果函数返回对象的析构(销毁)函数释放了资源(如内存或者文件句柄),这些资源会被释放,即使被赋了值对象还有使用它。这种问题的解决方案会涉及到使用拷贝构造函数,接下来会讨论。
最后一点,使用引用返回一个函数的对象也是可行的,但是要注意,在函数结束的时候被引用的对象不能够跑出作用域。
>>>>>>>>>>>>>>>>>>>>>>>translatedby ouyangjun
>>>>>>>>>>>>>>>>>>>>>>>e-mail:ouyangjun1985#msn.com
相关文章推荐
- opener 属性是一个可读可写的属性,可返回对创建该窗口的 Window 对象的引用
- 关于C++函数返回局部对象的详细分析
- 【Effection C++】读书笔记 条款28:避免返回handles指向对象内部成分
- SpringMVC 将复杂对象以json格式返回前端
- showmodule返回对象
- 遍历一个对象数组,返回一个包含相匹配的属性-值对的所有对象的数组
- 对象/对象指针返回(local stack\heap\temp对象)
- Java Getter方法返回类对象
- mybatis 查oralce 数据库之后 返回的java 对象部分字段没有注入数据,而数据库有数据
- 有关返回对象的说明------指向对象的引用、指向对象的const引用和const对象
- Oracle对象-包头,包体返回游标 多参数值
- 读书笔记 effective c++ Item 21 当你必须返回一个对象的时候,不要尝试返回引用
- 《Effect C++》学习------条款21:当必须返回对象时,别妄想返回其reference
- 函数返回一个SqlDataReader对象
- springMVC 【@response 返回对象自动变成json并且防止乱码】 & 【配置支持实体类中的@DateTimeFormat注解】
- 线程的锁函数:threading.Lock() 返回锁对象 Lock
- 11_5_3给people添加GetOldest()方法,使用上面定义的重载运算符,返回一个Age最大的对象数组
- springMVC传对象参数、返回JSON格式数据
- JAXWS学习(四)- 返回复杂对象