Is it possible to unhide only specific overloaded method of Base class - c++

#include <iostream>
class B
{
public:
virtual void f() {std::cout<<"Fi";}
virtual void f(int) {std::cout<<"GI";}
};
class A : public B
{
public:
void f(double) {std::cout<<"HI";}
using B::f;//but I want to use only f(), not f(int)
};
int main () {
A a;
a.f();
a.f(10);
return 0;
}
Is it possible in derived class A unhide only f() overload? So a.f(10); would call A::f(double)

You can replace the using statement with a function that calls the base class method:
void f() { B::f(); }

For what you are attempting, make A use protected or private inheritance instead so B's methods are not public in A. Then A can declare its own methods that call B's methods internally.
#include <iostream>
class B
{
public:
virtual void f() {std::cout<<"Fi";}
virtual void f(int) {std::cout<<"GI";}
};
class A : protected B
{
public:
void f(double) {std::cout<<"HI";}
void f() { B::f(); }
};
int main () {
A a;
a.f();
a.f(10);
return 0;
}
Output:
FiHI
Live Demo

Related

How to call a derived class' virtual function from the base class in C++

I have:
class A
{
public:
virtual some_type foo();
protected:
virtual some_type bar();
}
class B : public A
{
protected:
some_type bar() override;
}
// std::shared_ptr<B> b_ptr;
b_ptr->foo();
Where A::foo calls bar. For some reason that I cannot seem to understand, A::bar is being called insted of B::bar. Is there any way to ensure B::bar is called instead?
EDIT:
some_type A::foo() {
this->bar();
}
EDIT 2:
Class was instantiated as A, and then cast to B, hence the shared pointer to B was invalid.
It depends.
If foo() calls bar() in this way:
some_type foo(){ A::bar(); }
This is compile time binding, and so the compiler will bind that call to A::bar() even if the dynamic type is B.
Instead, if foo() calls bar() in this way:
some_type foo(){ this->bar(); /*or just bar(), same thing*/}
Then because bar() is virtual, the binding will be resolved at runtime, and so it will call the bar() function for the dynamic type, thus B::bar().
So this code:
class A
{
public:
virtual void foo(){this->bar();};
protected:
virtual void bar(){cout<<"A";};
};
class B : public A
{
protected:
void bar() override{cout<<"B";};
};
int main() {
std::shared_ptr<B> b_ptr(new B);
b_ptr->foo(); // B
}
Will print B, but this code:
class A
{
public:
virtual void foo(){A::bar();};
protected:
virtual void bar(){cout<<"A";};
};
class B : public A
{
protected:
void bar() override{cout<<"B";};
};
int main() {
std::shared_ptr<B> b_ptr(new B);
b_ptr->foo(); // A
}
will print A

Why override under private inheritance?

class Base {
public:
virtual void f() {}
};
class Derived : private Base {
public:
void f() override {}
};
My question is there any use to such override? Private inheritance implies that you can not store Derived in Base pointer and thus it will never be needed to dynamically dispatch f to the correct type.
Just one example: A function of Derived::f1() can call a (public or protected) functions of Base::f2(), which in turn can call f(). In this case, dynamic dispatch is needed.
Here is an example code:
#include "iostream"
using namespace std;
class Base {
public:
virtual void f() {
cout << "Base::f() called.\n";
}
void f2() {
f(); // Here, a dynamic dispatch is done!
}
};
class Derived:private Base {
public:
void f() override {
cout << "Derived::f() called.\n";
}
void f1() {
Base::f2();
}
};
int main() {
Derived D;
D.f1();
Base B;
B.f2();
}
Output:
Derived::f() called
Base::f() called

How to call a base class's virtual function that is an input argument to a function

using C++, I have
struct Base {
virtual void stuff(/*base stuff*/);
};
struct Derived : public Base {
void stuff(/*derived stuff*/);
};
void function1(Derived& obj){
obj.stuff();
}
In this scenario, function1 will use Derived's do() function. What if in function1, I want to call the Base class's do() function instead ? Will it work if I call function1 as
function1(dynamic_cast<Base*>(derived_obj_ptr))?
After correcting the multitude of errors in your code, this is indeed achievable:
#include <iostream>
class Base {
public:
virtual void foo() { std::cout << "Base\n"; }
};
class Derived : public Base {
public:
void foo() { std::cout << "Derived\n"; }
};
void function1(Derived *p) {
p->Base::foo(); // <<<<< Here is the magic >>>>>
}
int main() {
Derived d;
function1(&d);
}
Output:
Base
(see http://ideone.com/FKFj8)

Does a pointer to a virtual function still get invoked virtually?

Will a function pointer to a class member function which is declared virtual be valid?
class A {
public:
virtual void function(int param){ ... };
}
class B : public A {
virtual void function(int param){ ... };
}
//impl :
B b;
A* a = (A*)&b;
typedef void (A::*FP)(int param);
FP funcPtr = &A::function;
(a->*(funcPtr))(1234);
Will B::function be called?
Yes. Valid code to test on codepad or ideone :
class A {
public:
virtual void function(int param){
printf("A:function\n");
};
};
class B : public A {
public:
virtual void function(int param){
printf("B:function\n");
};
};
typedef void (A::*FP)(int param);
int main(void)
{
//impl :
B b;
A* a = (A*)&b;
FP funcPtr = &A::function;
(a->*(funcPtr))(1234);
}
Yes. It also works with virtual inheritance.
The function will be called, as you just try to invoke inherited function.
The best test for that thing is to make the methods in the class A a pure virtual method. In both cases (with or without pure virtual methods), B::function will be called.

pure virtual functions

In the C++ program:
#include<iostream.h>
class A
{
public: virtual void func()=0;
};
class B:public A
{
public: void show()
{
func();
}
};
void B::func()
{
cout<<"In B"<<endl;
}
int main()
{
B b;
b.show();
}
If the virtual function, func() is redefined within body of the class B, there is no error. But when using the scope resolution operator, the compiler throws an error.
Why is that?
This is not directly related to func being virtual, you always need to declare it in the class:
class B:public A
{
public: void show()
{
func();
}
void func(); // add this
};
void B::func()
{
cout<<"In B"<<endl;
}
You have to declare that you redefine the member function func() in class B.
class B:public A
{
virtual void func();
public:
void show() {func(); }
};