Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overriding Virtual Function clarification...

Reply
Thread Tools

Overriding Virtual Function clarification...

 
 
Achintya
Guest
Posts: n/a
 
      07-23-2005
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&
#include<iostream>
using namespace std;

class A
{
int i;
virtual void f(){ cout<<endl<<"i= "<<i; }
public:
A(int j) { i = j; }
};

class B : public A
{
int j;
virtual void g() { cout<<endl<<"j= "<<j; }
public:
virtual void f(){ cout<<"I'm in B::f()"; }
B(int k):A(k+10){ j = k; }

};

int main()
{
A* a_base;
B* b_derived;

a_base = new B(1);
a_base->f();
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&

Hi,

In the above code the virtual function declaration in A is causing a
compiler error since its private. Does this mean that the virtual
function overring requires the base delaration also to be public.
Kindly explain...as i hadn't tried this earlier.

-praveen.

 
Reply With Quote
 
 
 
 
Tobias Blomkvist
Guest
Posts: n/a
 
      07-23-2005
>
> In the above code the virtual function declaration in A is causing a
> compiler error since its private. Does this mean that the virtual
> function overring requires the base delaration also to be public.
> Kindly explain...as i hadn't tried this earlier.
>
> -praveen.
>


Private base class member functions are not accessable to derived
classes, try to make them protected instead.

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
 
Reply With Quote
 
 
 
 
Achintya
Guest
Posts: n/a
 
      07-23-2005


Tobias Blomkvist wrote:
> >
> > In the above code the virtual function declaration in A is causing a
> > compiler error since its private. Does this mean that the virtual
> > function overring requires the base delaration also to be public.
> > Kindly explain...as i hadn't tried this earlier.
> >
> > -praveen.
> >

>
> Private base class member functions are not accessable to derived
> classes, try to make them protected instead.
>
> Tobias


Hi,

Thanks..Thats fine, I tried that before posting here and was
successful. but I am interested in reason behind...

-praveen
> --
> IMPORTANT: The contents of this email and attachments are confidential
> and may be subject to legal privilege and/or protected by copyright.
> Copying or communicating any part of it to others is prohibited and may
> be unlawful.


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      07-23-2005
Achintya wrote in news:1122128095.067742.271170
@g43g2000cwa.googlegroups.com in comp.lang.c++:

> int main()
> {
> A* a_base;
> B* b_derived;
>
> a_base = new B(1);
> a_base->f();
> }
> &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&
>
> Hi,
>
> In the above code the virtual function declaration in A is causing a
> compiler error since its private. Does this mean that the virtual
> function overring requires the base delaration also to be public.
> Kindly explain...as i hadn't tried this earlier.
>


No, public is required as you call A::f() from main(), i.e. a
context where public access to A::f() is required.

When you make a call to A::f() the compiler doesn't care
wether A::f() is virtual, or if its been overriden, it only
checks the accessability of A::f().

The fact that in this case a_base actually points to a B with
an accessable f() doesen't matter. Type and access checking is
done by the compiler at compile time, not at runtime, and in
general the compiler doesn't know the dynamic type (B - above)
that a pointer (A *a_base - above) points too.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Tobias Blomkvist
Guest
Posts: n/a
 
      07-23-2005
> Hi,
>
> Thanks..Thats fine, I tried that before posting here and was
> successful. but I am interested in reason behind...
>
> -praveen
>


Everything declared private is supposed to be private, unaccessible to
others. Protected means that it is still shielded from outside access,
but accessible to derived classes. These are design options provided to
separate intentional functionality, thus making the interface clearer.

Additionally you can affect the inherited access level by deriving
with public, protected and private:

class x1 : public y {};
class x2 : protected y {};
class x3 : private y {};

Private is the default base class access specifier:

// private access, no of y's functions will be accessible to x4
class x4 : y {};

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
 
Reply With Quote
 
Girish Shetty
Guest
Posts: n/a
 
      07-25-2005
> #include<iostream>
> using namespace std;
>
> class A
> {
> int i;
> virtual void f(){ cout<<endl<<"i= "<<i; }
> public:
> A(int j) { i = j; }
> };
>
> class B : public A
> {
> int j;
> virtual void g() { cout<<endl<<"j= "<<j; }
> public:
> virtual void f(){ cout<<"I'm in B::f()"; }
> B(int k):A(k+10){ j = k; }
>
> };
>


The problem is, you are trying to change the access specifier of Base class
private function to public and trying to access it from mail using
polymorphism.
In C++, we can not increase the access of a variable/function from lower
level to upper level (example from private to public)

But, if your base class f() was public, then you can change your derived
class overloaded f()'s access specifier as private.

Now when you call your Derived function f() using base pointer, it does not
give you any error.

Regards
Girish


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-25-2005
* Tobias Blomkvist:
> >
> > In the above code the virtual function declaration in A is causing a
> > compiler error since its private. Does this mean that the virtual
> > function overring requires the base delaration also to be public.
> > Kindly explain...as i hadn't tried this earlier.
> >
> > -praveen.
> >

>
> Private base class member functions are not accessable to derived
> classes, try to make them protected instead.


Private virtual member functions can be overridden.

The problem in the OP's code is not that, but that he's trying to access a
member function through a pointer to a class where that function is private.

--
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
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-25-2005
* Girish Shetty:
> > #include<iostream>
> > using namespace std;
> >
> > class A
> > {
> > int i;
> > virtual void f(){ cout<<endl<<"i= "<<i; }
> > public:
> > A(int j) { i = j; }
> > };
> >
> > class B : public A
> > {
> > int j;
> > virtual void g() { cout<<endl<<"j= "<<j; }
> > public:
> > virtual void f(){ cout<<"I'm in B::f()"; }
> > B(int k):A(k+10){ j = k; }
> >
> > };
> >

>
> The problem is, you are trying to change the access specifier of Base class
> private function to public and trying to access it from mail using
> polymorphism.
> In C++, we can not increase the access of a variable/function from lower
> level to upper level


Sorry, that's incorrect.


> (example from private to public)


Right, the function must be accessible to do anything except overriding it.

Although, if the private function is virtual it can be overridden by a
forwarder function, which you can give any access specifier you want.

--
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
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-25-2005
* Alf P. Steinbach:
>
> Although, if the private function is virtual it can be overridden by a
> forwarder function, which you can give any access specifier you want.


Strike that. It can be overridden but of course not forwarded to. *banging
head against keyboard, consuming several litres of coffee to wake up*

--
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
 
Achintya
Guest
Posts: n/a
 
      07-25-2005
Hi,

Thanks all for dicussing this topic. Now I can reason out the cause.

Anybody can suggest good resource on net on how VTABLEs work?

-vs_p...

Alf P. Steinbach wrote:
> * Alf P. Steinbach:
> >
> > Although, if the private function is virtual it can be overridden by a
> > forwarder function, which you can give any access specifier you want.

>
> Strike that. It can be overridden but of course not forwarded to. *banging
> head against keyboard, consuming several litres of coffee to wake up*
>
> --
> 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
 
 
 
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
Template overriding base class virtual function? Peter Davis C++ 3 07-25-2012 01:06 PM
Overriding a virtual function and changing its return type --- dependency issues ctor C++ 13 03-09-2006 08:19 PM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM
Help me understand Overriding a virtual function CoolPint C++ 3 01-26-2004 12:45 PM
question regarding overriding of web.config in the root directory..in a web app in a virtual directory dotnetprogram ASP .Net 1 12-27-2003 06:02 AM



Advertisments