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; Hello, Is it a rule of thumb that private members of the class can never be accessed or modified outside ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    A silly question but it's my doubt ;)

    Hello,

    Is it a rule of thumb that private members of the class can never be accessed or modified outside the class??

    i mean..suppose my class is

    Code:
    class set
    {
        int x;
    
    };
    Is there any way that i can change or modify that variable from outside class?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Without using "tricks", no.

    Using tricks: Sure. Just take the address of the instance of the class, cast it to an integer pointer, and you've got a pointer to x [ you may need to increment the pointer if there are virtual functions in the class].

    --
    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.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    >>Using tricks: Sure. Just take the address of the instance of the class, cast it to an integer >>pointer, and you've got a pointer to x [ you may need to increment the pointer if there are >>virtual functions in the class].

    That kinda defeats the purpose of a class doesn't it?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Remember that variables are private when they are not supposed to be modified from the outside.
    If you DO need to access them from the outside, then make them public or make get/set functions.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    you can also control which other classes can access them by declaring friends

    Code:
    class set
    {
    friend class Friend;
    
    int x;
    };
    
    class Friend
    {
    Friend() { s.x = 10; }
    
    set s;
    };

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Use friends with care, however. They may break the entire encapsulation idea. It's better to expose members via which the other class can access data from the other.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by Elysia View Post
    Use friends with care, however. They may break the entire encapsulation idea. It's better to expose members via which the other class can access data from the other.
    true one good way to use friends is like how java does it. Every class in the same package gets access to the other classes in the package. In C++ I'd do this too on larger projects, declaring classes in the same module friends of each other.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Raigne View Post
    >>Using tricks: Sure. Just take the address of the instance of the class, cast it to an integer >>pointer, and you've got a pointer to x [ you may need to increment the pointer if there are >>virtual functions in the class].

    That kinda defeats the purpose of a class doesn't it?
    Sure, but the question was if you could modify x from outside the class, and yes, it's possible if you really want to - it's pretty pointless if you have free access to the source of the class itself, since you can just change the class itself [most of the time without any problems].

    But my point is that private is only useful as long as programmers "follow the rules".

    --
    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.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok why is the following program working? And if it's working then what's the sense of keeping the data members private?? If they can be accessed from outside class like this??

    Code:
    #include <iostream.h>
    class set
    {
      int x;
      public:
    	set()
    	{
    		x=10;
    	}
    	int& returnx()
    	{
    		return x;
    	}
    	void putdata()
    	{
    		cout<<x;
    	}
    };
    
    int main(void)
    {
     set T;
     int &t=T.returnx();
     t+=20;
     T.putdata();
    
     return 0;
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    true one good way to use friends is like how java does it. Every class in the same package gets access to the other classes in the package.
    There is the concept of "package private" in Java, which is a little different from friendship since friends can access private members, but other classes in the same package cannot access strictly private members, but they can access package private members.

    EDIT:
    Ok why is the following program working?
    It works because returnx() is public.

    And if it's working then what's the sense of keeping the data members private?? If they can be accessed from outside class like this??
    The better question to ask is: what is the purpose of having returnx() return a reference instead of using a getter/setter pair, or better yet, creating a function that does what the class is supposed to do? If there is no good reason, then do not return by reference.
    Last edited by laserlight; 04-23-2008 at 03:10 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Please somebody reply!!to my previous post

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    It works because your get function returns a reference. A much better way to do this is return a const reference if it's an object or return by value. This, in turn, makes sure the outside will not modify the members in the class unless they use a hack.

    The point of set/get may be that it increases encapsulation. You can add code inside those get/set functions later without breaking the code that uses the class.
    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.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    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?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Unfortunately, the compiler is not all-knowing. It has no idea what you're trying to do and can only warn about some perticular instances of code. The rest is up to you to find and catch.
    This is also one of the reasons there's const correctness. Make sure all variables that are not to be modified are const. Make sure all functions that that do not modify any data inside the class are const.
    If you make your get function return a const reference instead, you will get a compile error. The compiler caught your error.
    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.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Ok but we are always taught that private members are always inaccessible and this is accessible.
    No, x remains private. You could always rename x and client code will not break since they rely on returnx(), not x. However, since you return a reference, you cannot remove x, e.g., to compute the value currently stored in x when it is needed. This can be done if you use a getter/setter pair instead.

    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?
    There are valid uses for returning references. For example, you might be writing a container class, then it makes sense to return elements by reference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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