Confused with encapsulation

This is a discussion on Confused with encapsulation within the C++ Programming forums, part of the General Programming Boards category; I am trying to grasp encapsulation and I am getting confused on a few points: 1. Local variables in other ...

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

    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
    21,887
    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++.
    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

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    34
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 !
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,425
    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%.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    34
    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, 12: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, 02:08 PM

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