polymiorphism and access privilege ?

S

sun1991

I recently come to this thought, and did a test:

class B1
{
public:
virtual void Func()
{
std::cout << "Here is B1" << "\n";
}
};

class B2: public B1
{
private:
virtual void Func()
{
std::cout << "Here is B2" << "\n";
}
};

class B3: public B2
{
public:
virtual void Func()
{
std::cout << "Here is B3" << "\n";
}
};

int main()
{
B1* pB2 = new B2();
B1* pB3 = new B3();
pB2->Func();
pB3->Func();

delete pB2;
delete pB3;

return 0;
}
 
B

Barry

sun1991 said:
I recently come to this thought, and did a test:

class B1
{
public:
virtual void Func()
{
std::cout << "Here is B1" << "\n";
}
};

class B2: public B1
{
private:
virtual void Func()
{
std::cout << "Here is B2" << "\n";
}
};

class B3: public B2
{
public:
virtual void Func()
{
std::cout << "Here is B3" << "\n";
}
};

int main()
{
B1* pB2 = new B2();
B1* pB3 = new B3();
pB2->Func();
pB3->Func();

delete pB2;
delete pB3;

return 0;
}

Access control has nothing to do with polymorphism,
Your code compiles, because all the function you call (Func) is a member
of B1 , which is *public*; and pB2 and pB3 are both pointers to B1.
Which Func to called is to be decided during *run-time*.

C++ is more flexible on the derived class with its choosing access
control for overridding functions

In C# and Java,
You can't override a public function with protected/private access.
 
V

Victor Bazarov

sun1991 said:
I recently come to this thought, and did a test:

class B1
{
public:
virtual void Func()
{
std::cout << "Here is B1" << "\n";
}
};

class B2: public B1
{
private:
virtual void Func()
{
std::cout << "Here is B2" << "\n";
}
};

class B3: public B2
{
public:
virtual void Func()
{
std::cout << "Here is B3" << "\n";
}
};

int main()
{
B1* pB2 = new B2();
B1* pB3 = new B3();
pB2->Func();
pB3->Func();

delete pB2;
delete pB3;

return 0;
}

Because access specifiers and overriding are orthogonal.
Is that means when polymiorphism, the
access privilege is just up to base class?

It's up to the class through which you're calling it. In your case
'pB2' and 'pB3' are both pointers to 'B1', where the function is
declared public. If you make them pointers to 'B2', you won't be
able to call 'Func'.

V
 
R

Ron Natalie

C++ function calls are performed in this logical order:

1. First the name is looked up from the point of call.
2. Then the overloads for that name are examined for the best match.
3. Then the access is checked for the selected overload.
4. Then if the overloaded function is virtual, the final overrider is
substituted.

The access is checked before it determines virtuosity.
 
B

Barry

Ron said:
C++ function calls are performed in this logical order:

1. First the name is looked up from the point of call.
2. Then the overloads for that name are examined for the best match.
3. Then the access is checked for the selected overload.

I would draw a separate line here, the above is compile-time check on
the illegality of the called function, including return type, argument
list, access privilege checks, etc.

and all these are syntax sugar. as they are turned into binary code,
they are only data, and routines. No more C++ types, no more access
controls, ...

and the below is done during run-time. while at compile-time, the
compiler doesn't even know the derived class has override the virtual
function in the base class or not.
4. Then if the overloaded function is virtual, the final overrider is
substituted.

And the virtual function is not overloaded, nor the derived class does
not overload the virtual function in the based class,

13 [over]
1 When two or more different declarations are specified for a single
name *in the same scope*, that name is said to be overloaded.

the virtual function is actually called override

10.3 Virtual functions [class.virtual]
3. If a virtual member function vf is declared in a class Base and in a
class Derived, derived directly or indirectly from Base, a member
function vf with the same name and same parameter list as Base::vf is
declared, then Derived::vf is also virtual (whether or not it is so
declared) and it *overrides* Base::vf.

and the Derived class is called the "overrider"
 
H

Howard

Ron Natalie said:
C++ function calls are performed in this logical order:

1. First the name is looked up from the point of call.
2. Then the overloads for that name are examined for the best match.
3. Then the access is checked for the selected overload.
4. Then if the overloaded function is virtual, the final overrider is
substituted.

The access is checked before it determines virtuosity.

"Virtuosity"? How virtuous it is? I think the word would be "virtuality",
wouldn't it? :)

-H
 
S

sun1991

C++ function calls are performed in this logical order:

1. First the name is looked up from the point of call.
2. Then the overloads for that name are examined for the best match.
3. Then the access is checked for the selected overload.
4. Then if the overloaded function is virtual, the final overrider is
substituted.

The access is checked before it determines virtuosity.

Thank you, that's the exact answer I'm looking forward to!
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top