×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    87

    the function does not transfer ownership of the control to the caller

    the function does not transfer ownership of the control to the caller
    看文档的地时候有许多地方有这句话 深入理解不了 麻烦大家给解释一下
    包括ownership和ownership的transfer.

    不是需要翻译,是解释一下原理 谢谢

  2. #2
    Super Contributor
    Join Date
    Jun 2005
    Location
    Beijing
    Posts
    5,578

    Re: the function does not transfer ownership of the control to the caller

    ownership表示一个对象的所有权,可以有多个指针指向同一个对象,但是只能有一个拥有者负责删除这个对象,其它对象可以使用,便不负责删除。一个好的习惯是在使用指针做为函数参数时标明这个函数不是接收所有权,就像上面文档中提到的那样。
    Beover1984
    -----------
    Doing what comes naturally

  3. #3
    Super Contributor
    Join Date
    Feb 2006
    Location
    Beijing
    Posts
    4,361

    Re: the function does not transfer ownership of the control to the caller

    再举个小例子,比如:
    //the function does not transfer ownership of the control to the caller
    SomeClass* pObj = GetPointer();
    使用完pObj之后就不需要你delete pObj;

    如果是the function transfer ownership of the control to the caller,你就需要:
    SomeClass* pObj = GetPointer();
    // do something
    delete pObj;
    Stay Hungry. Stay Foolish.
    Sina Weibo:
    http://weibo.com/cxt728
    QQ Weibo:
    http://t.qq.com/Cxt_programmer

  4. #4
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    87

    Re: the function does not transfer ownership of the control to the caller

    Quote Originally Posted by Cxt_programmer View Post
    再举个小例子,比如:
    //the function does not transfer ownership of the control to the caller
    SomeClass* pObj = GetPointer();
    使用完pObj之后就不需要你delete pObj;

    如果是the function transfer ownership of the control to the caller,你就需要:
    SomeClass* pObj = GetPointer();
    // do something
    delete pObj;
    首先谢谢大家的回答,
    有什么机制能保证这一点呢 还是只能靠人为的注意?

  5. #5
    Super Contributor
    Join Date
    Feb 2006
    Location
    Beijing
    Posts
    4,361

    Re: the function does not transfer ownership of the control to the caller

    Quote Originally Posted by juniorhope View Post
    首先谢谢大家的回答,
    有什么机制能保证这一点呢 还是只能靠人为的注意?
    我认为这个应该是C/C++程序员自己保证
    Stay Hungry. Stay Foolish.
    Sina Weibo:
    http://weibo.com/cxt728
    QQ Weibo:
    http://t.qq.com/Cxt_programmer

  6. #6
    Super Contributor
    Join Date
    Jul 2007
    Location
    ShenZhen, China
    Posts
    4,346

    Re: the function does not transfer ownership of the control to the caller

    Quote Originally Posted by juniorhope View Post
    首先谢谢大家的回答,
    有什么机制能保证这一点呢 还是只能靠人为的注意?
    这种设计的名字叫做"约定", 约定控制权的转移与否, 少数情况下可以通过引用指针帮助减少犯错的机会, 或者如java全面接管对内存的管理, 但是对非内存资源就不起作用, 终究还是要程序员自己注意, 这是基本的编码要求.
    ----------------------------
    坚持学习, 坚持编码
    http://www.devdiv.net/
    qxiaoyuan

Similar Threads

  1. Music resources
    By LiveRock in forum Mobile Java General
    Replies: 9
    Last Post: 2007-12-01, 08:15
  2. Replies: 1
    Last Post: 2004-11-22, 04:41
  3. ActivateL() vs DrawNow()
    By tommylkc in forum Symbian User Interface
    Replies: 2
    Last Post: 2003-11-07, 11:08
  4. D211
    By arleth in forum Multimodecards
    Replies: 7
    Last Post: 1970-01-01, 02:00
  5. Problem with Nokia D211 Linux Drivers
    By fiveam in forum Multimodecards
    Replies: 1
    Last Post: 1970-01-01, 02:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×