Thread: A silly question but it's my doubt ;)

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    To return a reference in the way that you do in the example is a very good example of "how you can expose a private member by returning a reference". This is obviously a design decision that you can make if you want to - and you could change x by name or even how it's stored (you could store it inside a struct, or as a pointer for example) without changing the interface.

    But as Laserlight says, it's not recommended to do so.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Apr 2008
    Quote Originally Posted by Elysia View Post
    The point of set/get may be that it increases encapsulation.
    Only marginally.

    You can add code inside those get/set functions later without breaking the code that uses the class.
    But you've cast in stone the type(s) of your internal class representation.

  3. #18
    Join Date
    Oct 2007
    Inside my computer
    I've always been somewhat against get/set in the first place
    Although now I use a little workaround. The benefits of no get/set and yet the benefits of get/set in one package using a templated class as public member variabal instead.
    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.

  4. #19
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    Quote Originally Posted by chottachatri View Post
    Ok but we are always taught that private members are always inaccessible and this is accessible. True, Laser you should never return reference! then Is it not the reponsibility of compiler to warn or throw errors when such contradictory code is there?
    Then maybe they taught you in a slightly confusing way.
    If you try to access x directly, the compiler won't let you since it's private, but a public member function that chooses to give you access to x indirectly is perfectly valid.

    If you tell me your password and tell me it's supposed to remain private; there's nothing stopping me from posting it on the internet for everone to see.

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Your bank account is private. Only you can change the account type or withdraw money or check your balance.
    However you're free to give your bank account number to someone else so that they can make a deposit.
    You wouldn't want everyone to be able to withdraw money from it without your knowledge, which is why you keep it private.
    Make sense?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. silly printf("%d", var) question
    By nacho4d in forum C Programming
    Replies: 2
    Last Post: 10-30-2008, 04:08 PM
  2. Silly question but..
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2008, 12:39 PM
  3. Really quick silly question.
    By Jozrael in forum C++ Programming
    Replies: 36
    Last Post: 04-04-2008, 08:31 AM
  4. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM