Ambiguity in static polymorphism [duplicate] - c++

This question already has answers here:
name hiding and fragile base problem
(3 answers)
Closed 7 years ago.
Quite a simple question actually but one which is bugging me a lot and I'm unable to find a sound answer for it.
How is the function call d.show(); NOT ambiguous for the following code AND why does b->show(); after b = &d; leads to the calling of Base_::show()?
#include <iostream>
using std::cout;
class Base_
{
public:
void show()
{
cout<<"\nBase Show";
}
};
class Derived_ : public Base_
{
public:
void show()
{
cout<<"Derived Show"<<endl;
}
};
int main()
{
Base_ b;
Derived_ d;
b->show();
d.show();
}

This is called "Name Hiding" in C++. Essentially, if a class defines a function, it hides all functions with the same name from parent classes. Also worth noting, if you had a Base_* to a derived object, and called that function, it would call the base classes version as it is not virtual and will not attempt to find overrided implementations in derived classes.

Related

Calling a function of the derived class from a variable type of parent class [duplicate]

This question already has answers here:
Unable to call derived class function using base class pointers
(3 answers)
Base-class pointer pointing to a derived class cannot access derived-class method
(3 answers)
Closed 4 months ago.
I am learning Object Oriented Programming. I wrote the following simple program, but I don't follow why the call b->derived_function(); doesn't work. I understand the base class Base does not have a member function with that name, but since I do: b = new Derived();, shouldn't b be an object of the Derived class (making the function call valid)?
#include <iostream>
using namespace std;
class Base {
public:
virtual void base_function() = 0;
};
class Derived: public Base {
public:
void base_function() {
cout<<"base_function"<<"\n";
}
void derived_function() {
cout<<"derived_function"<<"\n";
}
};
int main() {
Base* b;
b = new Derived();
// b->base_function(); //works
b->derived_function(); //doesn't work; why?
return 0;
}
Here's a repro on ideone.com. Thanks!

C++ Overload & Override - cannot initialize a parameter of type '' with an rvalue of type '' [duplicate]

This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 5 years ago.
I'm normally a C# guy, and it handles this fine, largely under its handling of "best match" resolving the method to call. I'm trying to do something similar in C++ now, but am getting a compile error. Long story short, it's a combination of method overloading and overriding.
class Bar : public Foo { } //Contents don't really matter here
class Base
{
public:
virtual void Do(Foo* foo) { }
virtual void Do(Bar* bar) { }
};
class Derived : public Base
{
public:
virtual void Do(Bar* bar) { }
}
Foo* foo = new Foo();
Derived* d = new Derived();
d->Do(foo); //cannot initialize a parameter of type 'Bar*' with an rvalue of type 'Foo*'
So, it tries to resolve the method against only the methods in Derived rather than recognizing the base class implements a valid match. Again, C# finds the base method. Can C++ not do this, or am I missing something?
A function in a derived class hides any functions of the same name in a base class (even if you're overriding a virtual function). To unhide the other overloads, use a "using" statement:
class Derived : public Base
{
public:
virtual void Do(Bar* bar) { }
using Base::Do;
};

C++ - unwanted use of base method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a base class A and a few derived classes B, C and D, that all have a method DoSomething(), which is virtual in the base class method (it's implemented in all sub classes as well as in the base class).
I have a problem, that the derived class B uses the method from the base class A. This may be the result of bad design, but I do not see the problem as the implementation is pretty straight forward.
An object of class B that is created in the following
A* a = new B();
If I call the method DoSomething() for this object, the method of the base class is used:
a->DoSomething(); //Results in Base class method being called.
But I expect/want that the method of class B is used. Can you tell me what's wrong?
According to the symptoms that you describe:
either you have forgotten the virtual keyword in the base class member definition.
or you have a subtle difference in you signature in the derived class.
The way forward would be to indicate in the derived class that the function is an override:
class A {
...
virtual void DoSometing();
};
class D : public A {
...
void DoSomething() override;
};
In this case, in case of mismatch you'll get a clear compiler error message.
The following example implements what you asked for.
#include <iostream>
class A
{
public:
virtual void DoSomething()
{
std::cout << "A::DoSomething" << std::endl;
}
};
class B : public A
{
public:
virtual void DoSomething()
{
std::cout << "B::DoSomething" << std::endl;
}
};
int main(int argc, char **argv)
{
A *a = new B();
a->DoSomething();
delete(a);
return 0;
}
Output:
B::DoSomething

Best way to call hidden base class method [duplicate]

This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 9 years ago.
In the following code, "d.Foo()" throws a compiler error claiming that function Foo() does not take 0 arguments. Yet a 0-argument function with that name exists in the base class. The line "d.Base::Foo()" is acceptable.
I have a vague memory of learning that the use of a function name in a derived class hides all functions of that name in a base class, even though arguments may be different. I don't remember why, nor do I remember the best way to avoid that problem. Is my solution best, or is there another way to get at Base::Foo()?
Thanks very much!
RobR
// Override.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
class Base
{
public :
void Foo()
{
}
};
class Derived : public Base
{
public:
void Foo(int x)
{
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived d;
d.Foo();
d.Base::Foo();
return 0;
}
you can use(!) base class member functions via using
class Derived : public Base {
public:
using Base::Foo;
};
You could define Derived::Foo() as:
class Derived : public Base {
public:
void Foo() { Base::Foo(); }
};

C++ Constructor not calling [duplicate]

This question already has answers here:
Why is there no call to the constructor? [duplicate]
(3 answers)
Most vexing parse
(1 answer)
What does A a() mean? [duplicate]
(2 answers)
Closed 8 years ago.
#include<iostream>
using namespace std;
class base {
public:
base() {
cout<<"Constructing base \n";
}
virtual ~base() {
cout<<"Destructing base \n";
}
};
class derived: public base {
public:
derived() {
cout<<"Constructing derived \n";
}
~derived() {
cout<<"Destructing derived \n";
}
};
int main(void) {
derived d();
return 0;
}
Why in this program its not calling constructor?
Can Anyone explain?
.......
The problem is that you are declaring a function here:
// function d(), returns a derived object.
derived d();
What you need is
derived d; // C++03, C++11
or
derived d{}; // C++11 only
This is an "interesting" aspect of C++, where anything that can be parsed as a function declaration will be (provided it is in a context where a function can be declared).
See more on variable initialization in GoTW #1 Variable initialization - or is it?.
Try like this:
int main(void) {
derived d;
return 0;
}
When you type derived d() you are declaring a function and not creating object.
Declaration not proper for derived d();
Change it to:
int main(void)
{
derived d; // note the difference
return 0;
}
What you are doing -> derived d(), it declares a function. that returns instance of derived class.
See this for details: http://en.wikipedia.org/wiki/Most_vexing_parse
It is because derived d() is treated as "function d, called without any arguments, returning instance of derived". Google 'most vexing parse'.
int main(void)
{
derived d(); //here you are actually declaring a function
return 0;
}
You should do it like this:
derived d;
Then it will work.
In C++, before a compiler calls a function, it must be aware of the function. Either you need to write the function in the flow before it is called or it should at least be declared before the function is called. So by calling derived d() you have declared a function called d() with return type as derived object, you never created derived object. it should have been like,
int main() {
derived d;
return 0
}