need help with a method

This is a discussion on need help with a method within the C++ Programming forums, part of the General Programming Boards category; operator + must return an object an not a reference. For a detailed explanation consult the bible part 1 (effective ...

  1. #16
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    operator + must return an object an not a reference.

    For a detailed explanation consult the bible part 1 (effective C++ by Scott Meyers) item 23 (Dont try to return a reference when you must return an object).
    Why can't you just return a reference if that's effecient?
    Because if you return a HOLE object you actually copy the hole thing while if you only return a reference you just copy the adress.
    Last edited by Zahl; 10-28-2002 at 10:59 AM.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #17
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Zahl
    Why can't you just return a reference if that's effecient?
    Because if you return a HOLE object you actually copy the hole thing while if you only return a reference you just copy the adress.
    Correct - it isnt as efficient, but when you return a reference, the objects you are refering to will have its destructor called before you have the oportunity to use it (it goes out of scope), therefore your code is refering to an object that no longer exists - a sure way to mess things up!!

  3. #18
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok so we decided that if you were to return a reference u would have to use new within the function. So why is that not ok. Consider this.....
    Code:
    text& operator + (const text& Text1,const text& Text2)
    {
    text* NewText=new text;
    // concat Text1 and Text2
    return *NewText;
    }
    now what happens when we add these text objects together.
    Code:
    text T1("foo");
    text T2("bar");
    text T3("wont");
    text T4("do");
    text T5;
    T5=T1+T2+T3+T4;
    How many temporary objects are created in that last expression and who called delete on the newed memory they contain??
    That is why operator + MUST return an object. A reference is never suitable.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 05:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 08:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21