inheritance and composition

Discussion in 'C++' started by Gary Wessle, Nov 16, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    class Client {
    public:
    Client();
    virtual void login()=0;
    };
    Client::Client(){}

    class Play : public Client {
    public:
    Play();
    void login();
    };
    Play::play() Client(){}
    void Play::login(){ /*...*/};

    class Work : public Client {
    public:
    Work();
    void login();
    };
    Work::Work() Client(){}
    void Work::login(){ /*...*/};

    class Conn_task {
    protected:
    Work wo;
    Play pl;
    void est_con();
    public:
    Conn_task();
    };
    Conn_task::Conn_task(){}
    void Conn_task::est_con(){
    if( some_bool_value) wo.login(); else pl.login();
    }

    class Spac_task : public Conn_task {
    public:
    void preform();
    };
    void Spac_task::preform(){
    est_con();
    }

    my question is: how can I move the definition of login to the
    real-estate of Client and be sure Client knows which class type
    called it when est_con() is called?

    thanks
     
    Gary Wessle, Nov 16, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Guest

    Gary Wessle wrote:
    > class Client {
    > public:
    > Client();
    > virtual void login()=0;
    > };
    > Client::Client(){}
    >
    > class Play : public Client {
    > public:
    > Play();
    > void login();
    > };
    > Play::play() Client(){}
    > void Play::login(){ /*...*/};
    >
    > class Work : public Client {
    > public:
    > Work();
    > void login();
    > };
    > Work::Work() Client(){}
    > void Work::login(){ /*...*/};
    >
    > class Conn_task {
    > protected:
    > Work wo;
    > Play pl;
    > void est_con();
    > public:
    > Conn_task();
    > };
    > Conn_task::Conn_task(){}
    > void Conn_task::est_con(){
    > if( some_bool_value) wo.login(); else pl.login();
    > }
    >
    > class Spac_task : public Conn_task {
    > public:
    > void preform();
    > };
    > void Spac_task::preform(){
    > est_con();
    > }
    >
    > my question is: how can I move the definition of login to the
    > real-estate of Client and be sure Client knows which class type
    > called it when est_con() is called?
    >
    > thanks


    Use Virtual inheritance for your Client class as a first because right
    know you have 2 instances of client floating around. Basically when you
    use virtual inheritance, the most derived class needs to call the
    constructor of the virtual base class. You could use a protected member
    variable in the Client class that could be set by the resp child
    classes in their constructor and that could be used whenever login
    function in the client is called.
    There could be other solutions but this what popped up in my mind when
    I saw the minimal code that was posted.
     
    , Nov 16, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Radu Guest

    On Nov 16, 4:49 pm, Gary Wessle <> wrote:
    > class Client {
    > public:
    > Client();
    > virtual void login()=0;};Client::Client(){}
    >
    > class Play : public Client {
    > public:
    > Play();
    > void login();};Play::play() Client(){}
    > void Play::login(){ /*...*/};
    >
    > class Work : public Client {
    > public:
    > Work();
    > void login();};Work::Work() Client(){}
    > void Work::login(){ /*...*/};
    >
    > class Conn_task {
    > protected:
    > Work wo;
    > Play pl;
    > void est_con();
    > public:
    > Conn_task();};Conn_task::Conn_task(){}
    > void Conn_task::est_con(){
    > if( some_bool_value) wo.login(); else pl.login();
    >
    > }class Spac_task : public Conn_task {
    > public:
    > void preform();};void Spac_task::preform(){
    > est_con();
    >
    > }my question is: how can I move the definition of login to the
    > real-estate of Client and be sure Client knows which class type
    > called it when est_con() is called?
    >
    > thanks


    you can use the Visitor pattern

    HTH
    Radu
     
    Radu, Nov 17, 2006
    #3
  4. Gary Wessle

    Guest

    wrote:
    > Use Virtual inheritance for your Client class as a first because right
    > know you have 2 instances of client floating around. Basically when you
    > use virtual inheritance, the most derived class needs to call the
    > constructor of the virtual base class. You could use a protected member
    > variable in the Client class that could be set by the resp child
    > classes in their constructor and that could be used whenever login
    > function in the client is called.
    > There could be other solutions but this what popped up in my mind when
    > I saw the minimal code that was posted.


    HUH ???????????????????????????????????????????
     
    , Nov 17, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    1
    Views:
    485
    John C. Bollinger
    Apr 11, 2005
  2. Code4u
    Replies:
    9
    Views:
    2,779
  3. Replies:
    8
    Views:
    543
    James Kanze
    Jul 29, 2007
  4. Replies:
    18
    Views:
    1,636
    Odalrick
    Nov 18, 2007
  5. Todd
    Replies:
    22
    Views:
    2,056
    Arne Vajhøj
    Apr 26, 2008
Loading...

Share This Page