How to disable some base class method?

P

Peter Lee

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?
 
I

Ian Collins

Peter said:
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?
 
P

Peter Lee

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;
};
 
I

Ian Collins

Peter said:
Ian Collins ¼g¹D¡G
Please don't top-post.
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.
 
P

Peter Lee

Ian Collins ¼g¹D¡G
Please don't top-post.

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...
 
I

Ian Collins

Peter said:
Ian Collins ¼g¹D¡G


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.
 
G

Grizlyk

Peter said:
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;
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top