Best Way To Approach This?

This is a discussion on Best Way To Approach This? within the C++ Programming forums, part of the General Programming Boards category; Hi, Now in my project i need a main GUI Manager class, also loads of individual gui_functionaility classes (such as ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    45

    Best Way To Approach This?

    Hi,

    Now in my project i need a main GUI Manager class, also loads of individual gui_functionaility classes (such as custom file Dialog gui).

    Now the GUI manager distributes these gui_functionaility classes allowing other classes to inherit this functionaility through the gui manager.

    How would I go about this, classes that have classes in it protected section...Can you even do that?

    Cheers
    Alex

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You would use inheritance. I don't think you need to use composition unless you have some has-a relationships. Is that what you were asking?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Quote Originally Posted by appleGuy
    How would I go about this, classes that have classes in it protected section...Can you even do that?
    Sure you can. It's called class nesting. One common usage is to create functors that allow a more specialized usage of the STL algorithms:

    Code:
    /*
    A simple class with a string data member and a function that returns its member.
    */
    
    class CItem {
    public:
        CItem() {};
        virtual ~CItem() {};
    
        std::string id() const { return id_; }
    
    private:
        std::string     id_;
    };
    
    /*
    CInventory is an array of CItems. In its private section a functor is created
    to help delete an element by the id data member of CItem.
    */
    
    class CInventory {
    public:
        CInventory();
        virtual ~CInventory() {};
    
        virtual CInventory& remove(const std::string&);
    
    protected:
        std::vector<CItem*> contents_;
    
    private:
        class is_ID {
            public:
                is_ID(const std::string& s): id_(s) {}
                bool operator()(const CItem* obj) const { return obj->id() == id_; }
            private:
                std::string id_;
        };
    
    };
    
    /*
    This is the definition of CInventory::remove(). In it, the algorithm find_if() is
    used with a predicate function. Our functor defined as a private member.
    This way it was possible to find elements based on CItem data members.
    /*
    
    CInventory& CInventory::remove(const std::string& str) {
    
        std::vector<CItem_Ptr>::iterator iter;
        iter = std::find_if(contents_.begin(), contents_.end(), is_ID(str) );
    
        if( iter == contents_.end() ) {
            std::cerr << '\n' << "Object is not in inventory." << std::endl;
            return *this;
        }
    
        // Ok to remove
        contents_.erase(iter);
    
        return *this;
    
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I don't see any class nesting there.

  5. #5
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    Class "is_ID" is declared in class "CInventory". Would that not be a nested class example?
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Aha.
    private:
    class is_ID {
    public:
    is_ID(const std::string& s): id_(s) {}
    bool operator()(const CItem* obj) const { return obj->id() == id_; }
    private:
    std::string id_;
    };

    };
    The lack of caps on the nested class threw me. Honestly didn't see it first time around.
    Hideous if you ask me.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Other than the fact I stripped most of the code from those classes to simplify them for that example, and the fact I changed back to regular pointers instead of the smart pointers I have in place (also to simplify the example), I don't see a problem with using functors as private members of a class. As long as they make sense, of course.

    Do you have a better option? I would honestly like to know. I have these in place in my code on a few classes.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    cool,

    Sounds like a good idea (class nesting).
    So if say 3 classes were nested in a class called say guiManager. Would everyting that directly inherits from guiManager be able to use the functionaility from the nested classes?

    Also are nested classes quite common and are there any disadvantages of using them?

    Edit: Also can you declare a class in a class and define it seperatly?

    Cheers
    Alex
    Last edited by appleGuy; 09-01-2006 at 05:33 AM.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Hard to define common in this case, I guess. Nested classes are seen in code, yes.

    Nested classes are used when the relationship between the surrounding class and the nested class is very close. Usually when the nested class is meant to help the implementation of the surrounding class.

    If the nested classes are defined (or declared) in the public or protected parts of the surrounding class, then derived members will have access to it. But again, the objective of a nested class is to provide help in the implementation of its surrounding class, so I guess public access nested classes are much less common.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    oK,

    Maybe the way im going about this is not the right way.

    To put it into perspective, how would an standard application do its GUI.
    Would It have a GUI_Manager Class that has many nested classes such as File Dialog, or would there just be a defined library of gui classes that get called upon when needed..

    Its really hard to explain..so bear with me

    Cheers
    Alex

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You would not want to nest classes such as FileDialog under a GUI Manager class. The number of classes necessary would be large, and there is no reason to nest them. Nesting is for helper classes specific to the parent class.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    I guess we are having a little hard time providing you a real answer to your question because you didn't define to us yet what do you mean by a GUI_Manager class. What do you plan to have this class do?

    Regardless, as Daved said, forget about nested classes for now. I already regreat having given you an example
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Quote Originally Posted by Mario F.
    I guess we are having a little hard time providing you a real answer to your question because you didn't define to us yet what do you mean by a GUI_Manager class. What do you plan to have this class do?

    Regardless, as Daved said, forget about nested classes for now. I already regreat having given you an example
    well the gui_manager class will will be called upon for specific gui functionaility, say if the program wants a file dialog, the gui_manager will distribute accordingly (by calling the relevent class).

    However im now thinking that this is pointless and less efficent..

    -Alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unsure on how to approach this concurrency problem
    By osiris^ in forum C# Programming
    Replies: 3
    Last Post: 04-29-2008, 11:47 PM
  2. Best approach
    By DV64h in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-10-2006, 02:24 PM
  3. Angle of approach for C++
    By mepaco in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-13-2002, 06:08 PM
  4. right approach!
    By tarun in forum C Programming
    Replies: 14
    Last Post: 08-19-2002, 08:00 PM
  5. software development approach...
    By dkt in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-16-2001, 09:15 PM

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