OO Design regarding returning a value from an object

This is a discussion on OO Design regarding returning a value from an object within the C++ Programming forums, part of the General Programming Boards category; Quick question regarding OO design: If I had a class that contains a list of items that are defined within ...

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

    OO Design regarding returning a value from an object

    Quick question regarding OO design:

    If I had a class that contains a list of items that are defined within a struct. The struct in this case does not contain any allocated memory, so a direct copy to another struct of this type should be possible. Would it be more efficient (and correct) to:

    a: use a method to return the value of one of the items members and thus have one corresponding method for each member of the struct.

    b: use a single method to return a copy of the struct that can either be copied to a temporary struct type, or directly referenced from the returning value. i.e.
    Code:
    result = object.mymethod().structmember;
    Thanks.
    Visual C++ .net
    Windows XP profesional

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I would return the whole struct, so you only have to do one function call to get all your data. It's probably more efficient.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Yeah that is what I was thinking. Excellent, thanks!
    Visual C++ .net
    Windows XP profesional

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    c) Pass a pointer to the struct?

    result = object.GetStructPointer()->structMemberA;

    When you pass by value, the object needs copying and that's inefficient--especially for large objects.
    Last edited by 7stud; 08-24-2003 at 02:59 AM.

  5. #5
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    So I could then:
    Code:
    STRUCTTYPE *foobar;
    
    foobar = object.mymethod();
    And play with the members through the pointer foobar. Cool
    Visual C++ .net
    Windows XP profesional

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    STRUCTTYPE *foobar;

    foobar = object.mymethod();

    Now I'm not so certain that's the right thing to do. By doing that you expose your class data member: it can be changed through the pointer, so essentially you've made it public. Maybe b) is the better design choice--returning an object will pass it by value, so changes can't be made to the struct members. If you don't need to change the struct members, I think that would be best afterall.
    Last edited by 7stud; 08-24-2003 at 05:24 AM.

  7. #7
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Yeah, that is a good point, I don't really want that struct to be modified outside of the scope of the object. Ok - thanks.
    Visual C++ .net
    Windows XP profesional

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Include Problems
    By loopshot in forum Game Programming
    Replies: 13
    Last Post: 02-17-2006, 02:22 PM
  2. Making a script language?
    By Blackroot in forum Game Programming
    Replies: 10
    Last Post: 02-16-2006, 01:22 AM
  3. How would you do this (object interlinking)
    By darksaidin in forum C++ Programming
    Replies: 7
    Last Post: 08-30-2003, 12:08 AM
  4. OO in C
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-05-2002, 10:02 PM
  5. Object Oriented Design help!! ;(
    By cpp4ever in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2001, 10:16 AM

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