Thread: Returning a value from a member function

  1. #1
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87

    Returning a value from a member function

    Heya.

    I have a class in which part of the responsibilities of one of the members is to return an object of a specific type.

    Now I can't guarentee that I am going to know where these objects are going to end up and I don't want to end up with a memory leak.

    What do most people do when faced with this situation. Do you declare a static object and return a reference to it, do you allocate memory for it and ensure you clean up later, or do you prefer to return copies of the object (or do you not get in a situation where you don't know whether you are going to be able to clean up the objects later!)?

    The reason I ask is I am currently faced with a design decision in my project, and while this is a very open ended question, I am hoping to get some idea as to how others treat this kind of problem.

    Cheers
    Visual C++ .net
    Windows XP profesional

  2. #2
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    I guess you could use an auto_ptr so the object will at least be deleted at program termination.
    [code]

    your code here....

    [/code]

  3. #3
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Yeah, *slaps head* that is a good point. I may do that. Thanks
    Visual C++ .net
    Windows XP profesional

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    std::auto_ptr or boost::shared_ptr.

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Long back i used to build a stack and push all the pointer address inot it(or when ever it was created).. and use the destructor to clear it all up....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. member member function pointers
    By Bigbio2002 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2005, 05:14 PM
  4. problem returning struct member from function
    By yukster in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 01:21 PM
  5. Calling a member function from a member function?
    By Chroder in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2002, 11:19 AM