您的位置:首页 > 移动开发 > Objective-C

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:



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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息