A silly question but it's my doubt ;)

This is a discussion on A silly question but it's my doubt ;) within the C++ Programming forums, part of the General Programming Boards category; To return a reference in the way that you do in the example is a very good example of "how ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    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
    Posts
    890
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #19
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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
    Location
    New Zealand
    Posts
    6,269
    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"

Page 2 of 2 FirstFirst 12
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 Cprogramming.com
    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, 08:22 PM

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