Calling a Private function using Virtual Instance

P

Pravin

Hi all,

please look at this code

"

class VirtualBase{

public:
virtual int method1() = 0;
virtual int method2()=0;
virtual int method3()=0
};


class DerivedBase:public VirtualBase
{
public:
int method1(){
return 2;
}
int method2(){
return 2;
}
private:
int method3(){
return 2;
}
};

void main(){


DerivedBase ObjDerivedBase;
VirtualBase *ObjVirtualBase;

ObjVirtualBase = new DerivedBase;
cout<<"\nReturn:"<<ObjVirtualBase->method3();
}

"

Here..inspite of DerivedBase::method3() being private, i can still
call it using a pointer to VirtualBase after assigning a object of
DerivedBase.

I have difficulty understanding how this could be possible?
 
U

utab

Hi all,

please look at this code

"

class VirtualBase{

public:
virtual int method1() = 0;
virtual int method2()=0;
virtual int method3()=0

};

class DerivedBase:public VirtualBase
{
public:
int method1(){
return 2;
}
int method2(){
return 2;
}
private:
int method3(){
return 2;
}

};

void main(){

DerivedBase ObjDerivedBase;
VirtualBase *ObjVirtualBase;

ObjVirtualBase = new DerivedBase;
cout<<"\nReturn:"<<ObjVirtualBase->method3();

}

"

Here..inspite of DerivedBase::method3() being private, i can still
call it using a pointer to VirtualBase after assigning a object of
DerivedBase.

I have difficulty understanding how this could be possible?

I am not sure on the things that I will write, but I got this
impression out of some tries, the knowledgeable here would make it
more clear for you, I also wondered the result of this post. When you
inherit from an abstract base class, the members should be defined in
the derived classes, but since you can call the private member
function of a derived class through an abstract base class pointer
made me to think that since the called function is in the public
interface of the abstract base class, there is some kind of privilege
perhaps. I am unaware of this however.
 
K

Kai-Uwe Bux

Pravin said:
Hi all,

please look at this code

"

class VirtualBase{

public:
virtual int method1() = 0;
virtual int method2()=0;
virtual int method3()=0
};


class DerivedBase:public VirtualBase
{
public:
int method1(){
return 2;
}
int method2(){
return 2;
}
private:
int method3(){
return 2;
}
};

void main(){


DerivedBase ObjDerivedBase;
VirtualBase *ObjVirtualBase;

ObjVirtualBase = new DerivedBase;
cout<<"\nReturn:"<<ObjVirtualBase->method3();
}

"

Here..inspite of DerivedBase::method3() being private, i can still
call it using a pointer to VirtualBase after assigning a object of
DerivedBase.

I have difficulty understanding how this could be possible?

Access (public, private, protected) only relates to the static types. Since
*ObjVirtualBase is of static type VirtualBase, it allows you to access the
public member function method3. The dynamic type only determines which
implementation will be dispatched; it does not determine whether the member
function is accessible. See [11.6/1] for details.


Best

Kai-Uwe Bux
 
R

red floyd

Pravin said:
[redacted]
void main(){


DerivedBase ObjDerivedBase;
VirtualBase *ObjVirtualBase;

ObjVirtualBase = new DerivedBase;
cout<<"\nReturn:"<<ObjVirtualBase->method3();
}

Totally unrelated to your question, but I'm guessing you're just learning.

Your code is ill-formed, and should generate a diagnostic out of your
compiler.

main returns int. Period. Nothing else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top