How to disable some base class method?

Discussion in 'C++' started by Peter Lee, Jan 19, 2007.

  1. Peter Lee

    Peter Lee Guest

    class Base {
    public:
    void t1() {}
    void t2() {}
    void t3() {};

    };


    class Derived : public Base
    {
    public:

    private:
    using Base::t1;
    using Base::t3;

    };

    Derived d;

    d.t1(); // error, this is what I want
    d.t3(); // error, this is what I want
    d.Base::t1(); // ok <== I don't want user call method this way

    my question:
    I don't want user to call t1(); and t3(); from class Derived
    Is there any way?
    Peter Lee, Jan 19, 2007
    #1
    1. Advertising

  2. Peter Lee

    Ian Collins Guest

    Peter Lee wrote:
    > class Base {
    > public:
    > void t1() {}
    > void t2() {}
    > void t3() {};
    >
    > };
    >
    >
    > class Derived : public Base
    > {
    > public:
    >
    > private:
    > using Base::t1;
    > using Base::t3;
    >
    > };
    >
    > Derived d;
    >
    > d.t1(); // error, this is what I want
    > d.t3(); // error, this is what I want
    > d.Base::t1(); // ok <== I don't want user call method this way
    >
    > my question:
    > I don't want user to call t1(); and t3(); from class Derived
    > Is there any way?
    >

    There's nothing to stop them doing

    Base& b(d);
    b.t1();

    why do you want to do this?

    --
    Ian Collins.
    Ian Collins, Jan 19, 2007
    #2
    1. Advertising

  3. Peter Lee

    Peter Lee Guest

    Ian Collins ¼g¹D¡G

    I just wanto to disable Derived object call t1(); and t3();

    so this way is OK
    Base& b(d);
    b.t1();

    somethimes I want to inherit Base class that has many method
    but some Base class's method I don't want it ( the reason is maybe I
    think those method is NOT good or NOT safe .. etc)

    If I use the following way, it's waste time to copy and paste when Base
    class has many method
    But I just don't want some of them

    class D
    {
    public:
    void t2() { m_base.t2(); }

    private:
    Base m_base;
    };


    > Peter Lee wrote:
    > > class Base {
    > > public:
    > > void t1() {}
    > > void t2() {}
    > > void t3() {};
    > >
    > > };
    > >
    > >
    > > class Derived : public Base
    > > {
    > > public:
    > >
    > > private:
    > > using Base::t1;
    > > using Base::t3;
    > >
    > > };
    > >
    > > Derived d;
    > >
    > > d.t1(); // error, this is what I want
    > > d.t3(); // error, this is what I want
    > > d.Base::t1(); // ok <== I don't want user call method this way
    > >
    > > my question:
    > > I don't want user to call t1(); and t3(); from class Derived
    > > Is there any way?
    > >

    > There's nothing to stop them doing
    >
    > Base& b(d);
    > b.t1();
    >
    > why do you want to do this?
    >
    > --
    > Ian Collins.
    Peter Lee, Jan 19, 2007
    #3
  4. Peter Lee

    Ian Collins Guest

    Peter Lee wrote:
    > Ian Collins ¼g¹D¡G
    >

    Please don't top-post.

    >>Peter Lee wrote:
    >>


    >>>my question:
    >>>I don't want user to call t1(); and t3(); from class Derived
    >>>Is there any way?
    >>>

    >>
    >>There's nothing to stop them doing
    >>
    >> Base& b(d);
    >> b.t1();
    >>
    >>why do you want to do this?
    >>

    > I just wanto to disable Derived object call t1(); and t3();
    >
    > so this way is OK
    > Base& b(d);
    > b.t1();
    >
    > somethimes I want to inherit Base class that has many method
    > but some Base class's method I don't want it ( the reason is maybe I
    > think those method is NOT good or NOT safe .. etc)
    >
    > If I use the following way, it's waste time to copy and paste when Base
    > class has many method
    > But I just don't want some of them
    >

    Sounds a bit of a design smell to me. If that's what you want, make
    them private in Base and public in Derived.

    --
    Ian Collins.
    Ian Collins, Jan 19, 2007
    #4
  5. Peter Lee

    Peter Lee Guest

    Ian Collins ¼g¹D¡G

    > Peter Lee wrote:
    > > Ian Collins ¼g¹D¡G
    > >

    > Please don't top-post.
    >
    > >>Peter Lee wrote:
    > >>

    >
    > >>>my question:
    > >>>I don't want user to call t1(); and t3(); from class Derived
    > >>>Is there any way?
    > >>>
    > >>
    > >>There's nothing to stop them doing
    > >>
    > >> Base& b(d);
    > >> b.t1();
    > >>
    > >>why do you want to do this?
    > >>

    > > I just wanto to disable Derived object call t1(); and t3();
    > >
    > > so this way is OK
    > > Base& b(d);
    > > b.t1();
    > >
    > > somethimes I want to inherit Base class that has many method
    > > but some Base class's method I don't want it ( the reason is maybe I
    > > think those method is NOT good or NOT safe .. etc)
    > >
    > > If I use the following way, it's waste time to copy and paste when Base
    > > class has many method
    > > But I just don't want some of them
    > >

    > Sounds a bit of a design smell to me. If that's what you want, make
    > them private in Base and public in Derived.
    >
    > --
    > Ian Collins.


    Base class is not mine... it's maybe a system class framework...
    Peter Lee, Jan 19, 2007
    #5
  6. Peter Lee

    Ian Collins Guest

    Peter Lee wrote:
    > Ian Collins ¼g¹D¡G
    >
    >>Peter Lee wrote:
    >>
    >>>If I use the following way, it's waste time to copy and paste when Base
    >>>class has many method
    >>>But I just don't want some of them
    >>>

    >>
    >>Sounds a bit of a design smell to me. If that's what you want, make
    >>them private in Base and public in Derived.
    >>

    >
    > Base class is not mine... it's maybe a system class framework...
    >

    The make it a private base of Derived and expose the methods you want
    through Derived.

    --
    Ian Collins.
    Ian Collins, Jan 19, 2007
    #6
  7. Peter Lee

    Grizlyk Guest

    Peter Lee wrote:

    > d.t1(); // error, this is what I want
    > d.t3(); // error, this is what I want
    > d.Base::t1(); // ok <== I don't want user call method this way
    >
    > my question:
    > I don't want user to call t1(); and t3(); from class Derived
    > Is there any way?


    class Derived : protected Base
    {
    public:
    using Base::t2;
    };
    Grizlyk, Jan 19, 2007
    #7
    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. Teis Draiby
    Replies:
    3
    Views:
    449
    Pete Vidler
    Apr 2, 2004
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    544
    John Carson
    Sep 3, 2005
  3. Hicham Mouline
    Replies:
    1
    Views:
    592
    Victor Bazarov
    Apr 20, 2009
  4. Damien

    Disable ViewState on base class only?

    Damien, Jul 14, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    107
    Damien
    Jul 14, 2005
  5. Karan Rajput
    Replies:
    2
    Views:
    140
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page