By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,902 Members | 4,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,902 IT Pros & Developers. It's quick & easy.

Overriding Virtual Function clarification...

P: n/a
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&
#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.

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
>
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.
Jul 23 '05 #2

P: n/a


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.


Jul 23 '05 #3

P: n/a
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/
Jul 23 '05 #4

P: n/a
> 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.
Jul 23 '05 #5

P: n/a
> #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
Jul 25 '05 #6

P: n/a
* 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?
Jul 25 '05 #7

P: n/a
* 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?
Jul 25 '05 #8

P: n/a
* 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?
Jul 25 '05 #9

P: n/a
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?


Jul 25 '05 #10

P: n/a
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?


Jul 25 '05 #11

P: n/a

This discussion thread is closed

Replies have been disabled for this discussion.