Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why can't call a base member function from a object of sub class???

Reply
Thread Tools

Why can't call a base member function from a object of sub class???

 
 
Albright
Guest
Posts: n/a
 
      02-17-2009
Code as bellow:

#include <stdio.h>
#include <string.h>

class A
{
public:
void print(char *s)
{
printf("%s\n", s);
}
};

class B : public A
{
public:
void print(int i)
{
printf("%d\n", i);
}
};

int main()
{
B b;
b.print("hello"); //Has complile error here, it indicates that b
invoke B:rint() but not A:rint, I want to know why.

return 0;
}

In this sample, if print(char *s) is defined in class B, it's OK, but
if in A, it's not.
Why these two functions are NOT overloaded between base class and sub
class?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-17-2009
* Albright:
> Code as bellow:
>
> #include <stdio.h>
> #include <string.h>
>
> class A
> {
> public:
> void print(char *s)
> {
> printf("%s\n", s);
> }
> };
>
> class B : public A
> {
> public:
> void print(int i)
> {
> printf("%d\n", i);
> }
> };
>
> int main()
> {
> B b;
> b.print("hello"); //Has complile error here, it indicates that b
> invoke B:rint() but not A:rint, I want to know why.
>
> return 0;
> }
>
> In this sample, if print(char *s) is defined in class B, it's OK, but
> if in A, it's not.
> Why these two functions are NOT overloaded between base class and sub
> class?


This is a FAQ (Frequently Asked Question).

See the FAQ item titled "What's the meaning of, Warning: Derived::f(char) hides
Base::f(double)?", currently item 23.9 and available at e.g. <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9>, plus
at a host of mirror sites.

It's often a good idea to check the FAQ.


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
Albright
Guest
Posts: n/a
 
      02-17-2009
On Feb 17, 10:37*am, "Alf P. Steinbach" <al...@start.no> wrote:
> * Albright:
>
>
>
> > Code as bellow:

>
> > #include <stdio.h>
> > #include <string.h>

>
> > class A
> > {
> > public:
> > * *void print(char *s)
> > * *{
> > * * * * * *printf("%s\n", s);
> > * *}
> > };

>
> > class B : public A
> > {
> > public:
> > * *void print(int i)
> > * *{
> > * * * * * *printf("%d\n", i);
> > * *}
> > };

>
> > int main()
> > {
> > * *B b;
> > * *b.print("hello"); //Has complile error here, it indicates that b
> > invoke B:rint() but not A:rint, I want to know why.

>
> > * *return 0;
> > }

>
> > In this sample, if print(char *s) is defined *in class B, it's OK, but
> > if in A, it's not.
> > Why these two functions are NOT overloaded between base class and sub
> > class?

>
> This is a FAQ (Frequently Asked Question).
>
> See the FAQ item titled "What's the meaning of, Warning: Derived::f(char) hides
> Base::f(double)?", currently item 23.9 and available at e.g. <url:http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9>, plus
> at a host of mirror sites.
>
> It's often a good idea to check the FAQ.
>
> Cheers & hth.,
>
> - Alf


Thanks.

But Why the C++ standard has this rule?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-17-2009
* Albright:
> On Feb 17, 10:37 am, "Alf P. Steinbach" <al...@start.no> wrote:
>> This is a FAQ (Frequently Asked Question).
>>
>> See the FAQ item titled "What's the meaning of, Warning: Derived::f(char) hides
>> Base::f(double)?", currently item 23.9 and available at e.g. <url:http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9>, plus
>> at a host of mirror sites.
>>
>> It's often a good idea to check the FAQ.

>
> But Why the C++ standard has this rule?


Hm, properly that question belongs in [comp.std.c++]; here in [comp.lang.c++] we
mostly only deal with the language as-is.

However...

If C++ didn't have this rule it would have to have the opposite rule, at least
for this case, i.e. no hiding. That would be a different trade-off with its own
problems. For example, in that case adding a name in a base class could silently
(or more visibly) affect code using an existing derived class, requiring fixing
all the derived classes -- pretty hopeless when your class is in a library
used by many, and the classes to be fixed are the many's derived classes.

The rule that was adopted avoids e.g. that problem, at the cost of that rule
being somewhat counter intuitive, and generating a Frequently Asked Question.

A good textbook should explain this, but I don't know whether they do.


Cheers & hth.,

- Alf
 
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
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Can a sub-class (composite object) member function access private or protected members of base class? pkpatil@gmail.com C++ 2 06-09-2006 03:09 PM
How do you call a regular member function from a static member function? aling C++ 6 10-30-2005 04:38 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM



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