Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inheritance and name resolution

Reply
Thread Tools

inheritance and name resolution

 
 
Srini
Guest
Posts: n/a
 
      08-16-2005
Hello,

I've had this long lasting doubt. When a sub-class re-defines a
function that's in its superclass, it hides the superclass function.

class A {
public:
void foo(int) { }
};

class B : public A {
public:
void foo(std::string) { }
};

B obj;
obj.foo(10); // Error, no matching function B::foo(int)

Here B::foo(std::string) hides A::foo(int). So, if I want to bring
A::foo(int) into visibility, I've to put a using directive.

class B : public A {
public:
using A::foo; // bring A::foo(int) into scope here
void foo(std::string) { }
};

Now the normal overload function call resolution would happen. In that
case, why is the following not an error?

class A {
public:
void foo(int) { }
};

class B : public A {
public:
using A::foo;
void foo(int) { }
};

B obj;
obj.foo(10); // This would call B::foo(int)

Should this not result in a compile error because there are 2 functions
with the same signature?

Thanks,
Srini

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-16-2005
* Srini:
> why is the following not an error?
>
> class A {
> public:
> void foo(int) { }
> };
>
> class B : public A {
> public:
> using A::foo;
> void foo(int) { }
> };
>
> B obj;
> obj.foo(10); // This would call B::foo(int)


§7.3.3/12 explicitly allows this, stating that B::foo hides A::foo (the
example given is essentially the same as yours).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      08-16-2005

"Srini" <> wrote in message
news: oups.com...
> Hello,
>
> I've had this long lasting doubt. When a sub-class re-defines a
> function that's in its superclass, it hides the superclass function.
>
> class A {
> public:
> void foo(int) { }
> };
>
> class B : public A {
> public:
> void foo(std::string) { }
> };
>
> B obj;
> obj.foo(10); // Error, no matching function B::foo(int)
>
> Here B::foo(std::string) hides A::foo(int). So, if I want to bring
> A::foo(int) into visibility, I've to put a using directive.
>
> class B : public A {
> public:
> using A::foo; // bring A::foo(int) into scope here
> void foo(std::string) { }
> };
>
> Now the normal overload function call resolution would happen. In that
> case, why is the following not an error?
>
> class A {
> public:
> void foo(int) { }
> };
>
> class B : public A {
> public:
> using A::foo;
> void foo(int) { }
> };
>
> B obj;
> obj.foo(10); // This would call B::foo(int)
>
> Should this not result in a compile error because there are 2 functions
> with the same signature?
>
> Thanks,
> Srini
>


Consider:

class base
{
public:
void foo(int);
void foo(double);
void foo(std::string);
};

You are now require to override void base::foo(int), and also make the other
two overloads visible, how would you do?

class derived: public base
{
public:
using base::foo;

void foo(int);
};

Ben


 
Reply With Quote
 
Srini
Guest
Posts: n/a
 
      08-16-2005
Got it! A function in the derived class *always* hides the function
with the same signature, in the base class. Thank you Alf and Ben.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
module name versus function name resolution conflict. rocky Python 2 07-07-2009 09:44 AM
hardware resolution and optical resolution? JethroUK© Digital Photography 4 05-24-2008 03:20 PM
How do you change the Modelsim Cursor Resolution (not simulation resolution) Andrew FPGA VHDL 0 09-26-2005 04:05 AM
Scanning resolution, printing resolution, and downsampling hassy_user Digital Photography 11 10-27-2004 07:18 PM
ISO Resolution Chart and Printing Resolution Jack Yeazel Digital Photography 0 08-11-2003 11:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57