Thread: question about designing two classes in c++ without lessening encapsulation

  1. #1
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91

    question about designing two classes in c++ without lessening encapsulation

    dear all,
    i have two class:

    Code:
    class Robot
    {
        public:
            Robot(void)
            {
                c = new Canvas;
            }
    
            void send_to_canvas(DATA &d)
            {
                c->update_data(d);
            }
    
        private:
            Canvas *c;
            DATA data;
    };
    
    class Canvas
    {
        public:
            void update_data(DATA &d)
            {
                data = d;
            }
    
        private:
            DATA data;
    };
    so this is what those class will do:
    firstly, class Robot is directly exposed to the user/programmer
    then, class Robot will send the data as reference to class Canvas
    class Canvas will draw the data on gui window

    the problem is, class Canvas also has capabilities to interact to user/programmer by capturing mouse click event (for example). generally gui libraries work like this. when this event happens, it should modify the data both in class Robot and Canvas.

    Of course, I can do this easily by sending data as pointer (not as reference), right? but it means class canvas can modify data as it wishes and this will lessen encapsulation.

    What is the best way?
    shall I create a flag in class Canvas to tell class robot that mouse click event occurs, and class Robot will always check this flag so that data modification is done only by class Robot? this is an over killed solution.
    or shall I make class Canvas a friend class? friend class is also lessening encapsulation.

    I hope my question is clear.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The question is if it's really worth the effort? Two quick solutions I can think of are:

    - Let Canvas and Robot notify each other of changes. Prefer not to use the same data structure in this case. Let them only send over the required information and process it internally. This should increase encapsulation.
    - Let Canvas and Robot share a pointer (using std::shared_ptr) to the data. Quick and easy solution, but may reduce encapsulation.

    It's really about how far you want to go. There generally is no best way. There is always a tradeoff between encapsulation and efficiency (as in, amount of code to write).

    But why are you using a pointer to Canvas allocated with new in the first place? You're not even freeing it.
    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.

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    thanks for answering my questions, i think i understand.
    that's just brief part of the code, i do delete the canvas after finishing.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Delete calls in destructors usually make classes non-copyable (but in fact you can copy them by accident, which will result in segfault). You should consider mentioned shared_ptr. Reference members also make class non-copyable (copy operator will not be generated), but still copy-constructible, you shouldn't make them members too.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are a number of issues with raw pointers. Here are some:
    SourceForge.net: Raw pointer issues - cpwiki
    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.

  6. #6
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    Thanks every one.
    I am considering using singleton class to contain the data. The data is private member of this singleton class. Any other classes who try to read or modify this data must do it through some public methods in this singleton class.

    Of course there are disadvantages, but in my opinion this is the best way to minimize decoupling between classes.

    Eventhough the data is shared_ptr type, but if it is accessed by several methods in several different classes, i just feel it is not elegant enough.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This means that there can ever be only one Robot and one Canvas? If two instances need to share something, is the best solution to make that something essentially a global? What steps are you taking so that Data can't be modified anywhere outside Robot or Canvas?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    For the time being, I only have one robot on one canvas.

    Later I can improve by making data as an array of 16 (for example we limit the robot to 16).
    in robot class we add another varible "char id", when robot class connects to canvas class it give a unique id, whenever robot class wants to call public member of canvas class, robot class must also send its id as a parameter. This id will be used as an index to access array of data. Let's say robot1 can access data[1], robot2 can access data[2] and so on.

    Please, correct me if I am wrong, since they are clearly acceessing different area of memory, this won't make any serialization issue become necessary. Just my thinking, because I haven't gone this far.

    Actually this is just a unversity project, my robot name is Azolla, you can click on my signature , but that one was old, its design was not so good, decouplings are every where. Now I am making new one under linux system for probabilistic robotics implementation. This time I try to consoder seriously about OO design.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with winAPI in classes (newbie question)
    By Spyril in forum Windows Programming
    Replies: 3
    Last Post: 11-09-2007, 03:21 PM
  2. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 01:46 AM
  3. Replies: 2
    Last Post: 07-28-2006, 02:59 PM
  4. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 02:40 AM
  5. Question about classes
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-19-2002, 01:22 PM