LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2008, 02:32 PM   #1
santana
Member
 
Registered: Sep 2004
Distribution: FC, ubuntu, OpenSuse
Posts: 112

Rep: Reputation: 16
why is this method hidden?


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;
}
 
Old 11-07-2008, 02:43 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by santana View Post
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'.
 
Old 11-07-2008, 03:00 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
Old 11-07-2008, 03:33 PM   #4
santana
Member
 
Registered: Sep 2004
Distribution: FC, ubuntu, OpenSuse
Posts: 112

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by dmail View Post
For questions like this you really should do you own leg work.
So what are you quoting? If I had that reference I could do my own leg work
 
Old 11-07-2008, 03:52 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I quoted from the C++ standard which is not free, yet there are draft versions available for free such as this
 
Old 11-07-2008, 04:05 PM   #6
santana
Member
 
Registered: Sep 2004
Distribution: FC, ubuntu, OpenSuse
Posts: 112

Original Poster
Rep: Reputation: 16
You're right I really should be more familiar with the standard. Thanks for the link.
 
Old 11-07-2008, 04:31 PM   #7
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
Quote:
Originally Posted by dmail View Post
I quoted from the C++ standard which is not free, yet there are draft versions available for free such as this
How much leg work did it take to pinpoint the answer in this particular case?
 
Old 11-07-2008, 05:41 PM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by sharky View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Method Not Allowed: The requested method POST is not allowed for the URL /writedhcp.p WiWa Linux - Networking 15 01-06-2011 01:20 PM
Hidden GRUB! Edstorm Linux - Software 2 07-29-2008 11:18 AM
SlackWare :Method Not Allowed The requested method POST is not allowed for the URL slack31337 Linux - Software 0 04-08-2006 06:09 PM
hidden folders tenmiles Linux - General 5 11-23-2004 02:37 AM
Where is my bashrc hidden? Nikon01 Slackware 5 08-24-2004 02:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration