ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Any one explain why the following class hierarchy overrides the method with const argument? Shouldn't it be an overload, allowing both methods to exist side by side?
#include <iostream>
using namespace std;
class B
{
public:
virtual void SetBlah(const double d[3])
{ cerr << "B::SetBlah()" << endl; }
};
class D : public B
{
public:
void SetBlah(double d[3])
{ cerr << "D::SetBlah()" << endl; }
};
int main()
{
double p[3]={0.0};
const double q[3]={1.0};
D d;
d.SetBlah(p);
d.SetBlah(q); // exists in the base class, why hidden here?
d.B::SetBlah(q);
Any one explain why the following class hierarchy overrides the method with const argument? Shouldn't it be an overload, allowing both methods to exist side by side?
#include <iostream>
using namespace std;
class B
{
public:
virtual void SetBlah(const double d[3])
{ cerr << "B::SetBlah()" << endl; }
};
class D : public B
{
public:
void SetBlah(double d[3])
{ cerr << "D::SetBlah()" << endl; }
};
int main()
{
double p[3]={0.0};
const double q[3]={1.0};
D d;
d.SetBlah(p);
d.SetBlah(q); // exists in the base class, why hidden here?
d.B::SetBlah(q);
return 0;
}
I am not a C++ guy, but I think you still have access to base class methods through 'upper'.
For questions like this you really should do you own leg work.
Quote:
13.2 Declaration matching [over.dcl]
1 Two function declarations of the same name refer to the same function if they are in the same scope and
have equivalent parameter declarations (13.1). A function member of a derived class is not in the same
scope as a function member of the same name in a base class. [Example:
class B {
public:
int f(int);
};
class D : public B {
public:
int f(char*);
};
Here D::f(char*) hides B::f(int) rather than overloading it.
void h(D* pd)
{
pd->f(1); //error:
// D::f(char*) hides B::f(int)
pd->B::f(1); //OK
pd->f("Ben"); //OK, calls D::f
}
—end example]
To fix your example add "using B::SetBlah" in D to bring it into scope.
How much leg work did it take to pinpoint the answer in this particular case?
For me none, I knew where the explanation was in the standard. The problem I have generally is that some people ask first and if they get no answer look later (this is not totally directed at the OP seeing as he didn't have a copy of the standard), where as I and many other hackers are more inclined to look first and ask if you can not find a solution to a question. For example have a look at my profile and see how many questions I have asked. If the OP had the standard then a quick search in the pdf would reveal the answer with an example which is so close to the OP's it is uncanny.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.