Thread: Returning a class

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    Returning a class

    I have a class that needs to return a class (of the same type). (The real code is 400+ lines) Something like:
    Code:
    SampleClass SampleClass::Member(const SampleClass &c) const
    {
       SampleClass return_value;
    
       /* Some code that figures return_value */
       return return_value;
    }
    
    some_other_function()
    {
       SampleClass c, t = SampleClass(500), q;
    
      c = t.Member(q);
      // c != what it should
    }
    SampleClass has some dynamically allocated members, allocated in the constructor and deallocated in the destructor. However, whenever the function above is called, if I output the values in return_value immediately before the return statement, everything is fine. However, incorrect information is returned from the function.

    This seemed to work fine, until I compiled under a Linux environment. (Windows seems to let you get away with more.)

    The best my mind can figure is that return_value's destructor is called before the value is actually returned. (Since it's allocated on the stack.) The only way to avoid this that I can think of is to use pointers and dynamically allocate everything. Is there a Better Way?
    Last edited by Cactus_Hugger; 10-07-2005 at 09:36 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You did not mention a copy constructor. Have your written an appropriate copy constructor?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Returning a class object from a function returns (in principle at least) a copy of the object being returned. Check the copy constructor of your class to ensure it works as intended. If the class has some dynamically allocated members, a rough rule of thumb is that it will require a hand-rolled copy constructor that dynamically allocates copies of the appropriate members rather than the copy constructor that the compiler gives you by default. For good measure, if you have to write a copy constructor, you may also wish to write an assignment operator (eg a member SomeClass &operator=(const SomeClass &)).

    The reason you might get away with it for some compilers and not others is that returning a value from a class logically creates a temporary that is copied to the caller (hence the need for a working copy constructor). However, the C++ standard explicitly allows compilers some leeway: specifically, an acceptable optimisation is to avoid creating temporary if the only means of detecting that the temporary has existed is by manually tracking calls to constructors and destructor of the class. Not all compilers implement such an optimisation. The ones that don't will require the hand-rolled copy constructor in your case.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Sorry for the late reply. I read up on assignment operators, and that was my problem. The class works wonderfully now. (I did forget to mention I have a copy constructor - I'd been confusing it with the assignment operator. Now I understand their differences. (I have both now.))

    Many Thanks!
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. returning a class
    By apacz in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2005, 06:53 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM