returning reference

This is a discussion on returning reference within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by medievalelks Don't do it. Why do you want to return a reference to your private data? If ...

  1. #31
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by medievalelks View Post
    Don't do it. Why do you want to return a reference to your private data? If your object is really a POD, make it a struct.

    I'd also revisit the design. If you have a bunch of classes passing around and operating on a data struct, you may want encapsulate that data in an object with real behavior.
    Im a little confused about your post.. How else am I supposed to share the options across the different classes in my program? I thought this is the right approach towards that kind of problems in c++.

    I think the design I mentioned is good because in case options change, I dont have to change the data in each class separetly (if each class holds the options it needs).
    Last edited by l2u; 04-17-2008 at 10:45 AM.

  2. #32
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by l2u View Post
    Im a little confused about your post.. How else am I supposed to shared the options across the different classes in my program? I thought this is the right approach towards that kind of problems in c++.

    I think the design I mentioned is good because in case options change, I dont have to change the data in each class separetly (if each class holds the options it needs).
    It's hard to tell without a more concrete example. What are these "options"? Are these program-wide settings that you are managing with the options class? If so, how are they initialized?

    The way I've handled this is to create a singleton that manages all program settings, which are read from a text file and stored in a std::map. Clients of the class fetch object values by calling a getString() or getDouble() member function on the singleton, which returns the value of the setting or option.

  3. #33
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by medievalelks View Post
    It's hard to tell without a more concrete example. What are these "options"? Are these program-wide settings that you are managing with the options class? If so, how are they initialized?

    The way I've handled this is to create a singleton that manages all program settings, which are read from a text file and stored in a std::map. Clients of the class fetch object values by calling a getString() or getDouble() member function on the singleton, which returns the value of the setting or option.
    I dont like singleton design approach because I want to be more object oriented - to be able to have multi-session program (different options for same tasks).

  4. #34
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by l2u View Post
    I dont like singleton design approach because I want to be more object oriented - to be able to have multi-session program (different options for same tasks).
    What is not OO about singletons?

    Anyway, that solution is out since you want multiple instances of the options. In any event, I would hide options behind a queryable interface so you don't have multiple objects fiddling with what is essentially a POD. So instead of checking if options.FullScreen is true in different places, have them call OptionInterface.isFullscreen().

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    I like my own class member function interface better. It essentially makes it possible to use class members as PODs, yet they are object oriented in sense that you can wrap functionality in function calls. Essentially, it's like set/get.
    I posted it on the 1st page.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 05:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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