Thread: Confused with encapsulation

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    36

    Confused with encapsulation

    I am trying to grasp encapsulation and I am getting confused on a few points:

    1. Local variables in other functions of the same class
    When I want to access a variable which is local to another function of the same class, what do I have to do? I have read about accessor functions but I am not entirely sure if I get the concept.

    2. Can pointers get around encapsulation? They seem to be powerful althought I am not entirely getting them completeley. And by looking at some rather complex code from other programmers (which I am trying to figure out), pointers can happily grab stuff out of scope.

    3. Is there a comprehensive guide that specifically explains how to pass informations on different levels of scope? Trying to pass stuff around between different parts of the code has been a very confusing affair for me so far.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest reading How Non-Member Functions Improve Encapsulation. The focus of the article does not actually answer your questions, but hopefully you'll get a better picture of encapsulation and how it might be applied in C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Jefff View Post
    I am trying to grasp encapsulation and I am getting confused on a few points:

    1. Local variables in other functions of the same class
    When I want to access a variable which is local to another function of the same class, what do I have to do? I have read about accessor functions but I am not entirely sure if I get the concept.
    Not possible.
    Accessor functions are for accessing private members of the class.
    2. Can pointers get around encapsulation? They seem to be powerful althought I am not entirely getting them completeley. And by looking at some rather complex code from other programmers (which I am trying to figure out), pointers can happily grab stuff out of scope.
    Yes, they can. But that would be rather bad code to do do so.
    3. Is there a comprehensive guide that specifically explains how to pass informations on different levels of scope? Trying to pass stuff around between different parts of the code has been a very confusing affair for me so far.
    Err...There are only a few ways to pass stuff.
    (By value, by reference and by address (There is also 'moving' but that is not the point here)).
    You should have learnt those 3 in detail while learning about functions.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    I am reading through my c++ about proper use of pointers but the specific problem I am having here does not seem to appear in there. (That or I am overlooking something). The mostly talk about

    I have a function in a class that is using a pointer like so:
    Code:
    if (ptr->group == SomeIntegerValue)
    The function gets *ptr passed as a parameter from something else which I do not entirely understand yet. Therefore I plan to get around that by having another pointer(from another function in the same class) that accesses SomeIntegerValue like this.
    And what would be the correct syntax to do so?


    Yes, they can. But that would be rather bad code to do do so.
    I am working with a modding SDK for a large game engine and pointers fishing out things all over the place appears to be very common so I would like to learn how to do it properly.
    Last edited by Jefff; 06-10-2012 at 07:46 AM.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Jefff View Post
    I have a function in a class that is using a pointer like so:
    Code:
    if (ptr->group == SomeIntegerValue)
    The function gets *ptr passed as a parameter from something else which I do not entirely understand yet. Therefore I plan to get around that by having another pointer(from another function in the same class) that accesses SomeIntegerValue like this.
    And what would be the correct syntax to do so?
    I don't understand what you are trying to get around.
    The 'ptr' points to an object.
    group is a data member of that object.
    If won't be accessible if it is private, pointer or not. (If the object is of the same class, it'll be.)
    Since you say that existing code does that, it already is public, or friend-zoned !

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Jefff View Post
    I am reading through my c++ about proper use of pointers but the specific problem I am having here does not seem to appear in there. (That or I am overlooking something).
    Uh hu. More likely, you are not clear in your mind what your specific problem actually is.

    If you're not clear on that, you cannot describe it in a way other mere mortals can understand. None of the regular members here are omnipotent, unless they hide their capabilities very well, so nobody will be able to understand your problem enough to help you.

    Quote Originally Posted by Jefff View Post
    I have a function in a class that is using a pointer like so:
    Code:
    if (ptr->group == SomeIntegerValue)
    The function gets *ptr passed as a parameter from something else which I do not entirely understand yet. Therefore I plan to get around that by having another pointer(from another function in the same class) that accesses SomeIntegerValue like this.
    And what would be the correct syntax to do so?
    No idea. Your problem description here is completely unclear.

    Quote Originally Posted by Jefff View Post
    I am working with a modding SDK for a large game engine and pointers fishing out things all over the place appears to be very common so I would like to learn how to do it properly.
    In that case, why not find an example of how one of those other pointers are fishing things out? And do the same.

    From what you describe, however, that "large game engine" is not practicing encapsulation in any useful form. I interpret your expression "pointers fishing out things all over the place appears to be very common" as "pointers are being used to bypass encapsulation".

    That is a sign of either poor design of the game engine, or inappropriate usage of that game engine by hackers. Or both.

    You need to spend more time understanding both the design engine, and how other folks have used it .... and, more importantly, why they have used it that way. Then the answers to your question here (which, since you haven't provided enough information, we can't answer) will be moot.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    Following manasijs advice I practiced the basics a bit more an I came up with a solution.
    I copied the value from the pointer into a member variable which I created for this purpose which then could be accessed by other functions of the same class.

    However I still do not understand how the pointer was able to deliver the data to one of the functions in the first place.

    No idea. Your problem description here is completely unclear.
    I could post the relevant bits of the code here, but I did not want to do so because I feared looking like some idiot that is asking for someone else to do the hard work for me.



    In that case, why not find an example of how one of those other pointers are fishing things out? And do the same.

    From what you describe, however, that "large game engine" is not practicing encapsulation in any useful form. I interpret your expression "pointers fishing out things all over the place appears to be very common" as "pointers are being used to bypass encapsulation".

    That is a sign of either poor design of the game engine, or inappropriate usage of that game engine by hackers. Or both.
    I'll look deeper into it. Also I do not claim the engine is badly programmed or something. Probably me just having difficulty to understand more complex code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encapsulation Vs Abstraction
    By Rakesh Kp in forum C++ Programming
    Replies: 1
    Last Post: 09-13-2011, 02:30 AM
  2. Encapsulation in C++
    By JohnLeeroy in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2011, 05:02 PM
  3. encapsulation in c
    By swaugh in forum C Programming
    Replies: 2
    Last Post: 12-01-2004, 01:37 PM
  4. Integration or Encapsulation :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 06-02-2002, 09:14 AM
  5. Encapsulation window creation
    By xds4lx in forum Windows Programming
    Replies: 1
    Last Post: 11-09-2001, 03:08 PM