How to hide a public function inherit from base class
-
I have 2 classes, B inherit from A, but actually A have a function: fun should not present in B, so I code as blow,
class A{
public:
virtual void fun(){cout << "a" << endl; };
};class B : public A{
virtual void fun(){};
};But this take no effect, I can call the fun in class b,
int main()
{
A a;
B b;
A *pa = new B;a.fun(); b.A::fun(); pa->fun();
}
the output is
a
aHow to decline the function call? Thanks.
-
I have 2 classes, B inherit from A, but actually A have a function: fun should not present in B, so I code as blow,
class A{
public:
virtual void fun(){cout << "a" << endl; };
};class B : public A{
virtual void fun(){};
};But this take no effect, I can call the fun in class b,
int main()
{
A a;
B b;
A *pa = new B;a.fun(); b.A::fun(); pa->fun();
}
the output is
a
aHow to decline the function call? Thanks.
What is your point?
pa->fun()
doesn't fit your needs?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What is your point?
pa->fun()
doesn't fit your needs?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]yes, the A::fun() is bad for the B, will case error, so i want let A::fun to be public, but to B the fun must cannot be call
-
yes, the A::fun() is bad for the B, will case error, so i want let A::fun to be public, but to B the fun must cannot be call
What about (1) Writing
A
class withoutfun
function. (2) Writing a (say)AA
class that inherits fromA
and providesfun
function. (3) Writing aB
class that inherits fromA
class and therefore is not able to callfun
function? Of courseA
class consumers should becomeAA
class ones. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What about (1) Writing
A
class withoutfun
function. (2) Writing a (say)AA
class that inherits fromA
and providesfun
function. (3) Writing aB
class that inherits fromA
class and therefore is not able to callfun
function? Of courseA
class consumers should becomeAA
class ones. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]yeah, that's the better way to do, but in this project others may dislike this implement, they didn't want their code update, @_@. Thanks for all your help.
-
I have 2 classes, B inherit from A, but actually A have a function: fun should not present in B, so I code as blow,
class A{
public:
virtual void fun(){cout << "a" << endl; };
};class B : public A{
virtual void fun(){};
};But this take no effect, I can call the fun in class b,
int main()
{
A a;
B b;
A *pa = new B;a.fun(); b.A::fun(); pa->fun();
}
the output is
a
aHow to decline the function call? Thanks.
Hi, I don't understand what you don't want to happen for the B class. do you want to hide the functionaly from a b object or the declaration; like this:
int main() { A a; B b; a.fun(); // outputs "a" b.fun(); // compiles but doens't do something // or b.fun(); // doens't compile A* p = &b; p->fun(); // compiles but doens't do something b.A::fun(); // compiles but doens't do something }
Maybe you could use the Facade pattern to hide the unwanted A functionaly.
Learn from the mistakes of others, you may not live long enough to make them all yourself.