Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and why is it possible to change access level of a member?

Tags:

c++

oop

I ended up coding (With some help) something like this yesterday:

#include <iostream>

using namespace std;

class A
{
    public:
        virtual void foo(){cout << "A::foo\n";}
};

class B : private A
{
    private:
        virtual void foo(){ cout << "B::foo\n";}
        void DoSomething(SomeOtherClass& o){o.DoSomething(*static_cast<A*>(this));}
};

I tried changing the inheritance method:

class B : public A
{
    private:
        virtual void foo(){ cout << "B::foo\n";}
};

int main()
{
    A* a = new B;
    a->foo();
}

This still works. I expected a compile time error. Please tell me why this is possible and what are the possible uses? I know one use due to first scenario - You can expose different interfaces for different classes.

EDIT:

In the second case, output is B::foo.

like image 596
nakiya Avatar asked Nov 04 '10 07:11

nakiya


People also ask

How do I change the access level of a user?

To change the access level for a user or group, first remove them from their existing access level, and then add them to the access level you want them to have. Choose the user or group and then select Remove. Add the user or group to the other access level following the steps provided in the previous section.

How do I change access level from stakeholders to basic?

Navigate to https://youraccount.visualstudio.com/_admin/_users. Select an Basic user and select - Change Access level. Assign this user with Stakeholder. Select the desired stakeholder access user and change his/her access to Basic Access.

How should access levels be determined?

An organization's access levels are determined by its policies and procedures. These policies and procedures are typically written down and stored electronically. The policies and procedures document defines the rules that govern access levels.


2 Answers

using namespace std; 

class A 
{ 
    public: 
        virtual void foo(){cout << "A::foo\n";} 
}; 

class B : public A 
{ 
    private: 
        virtual void foo(){ cout << "B::foo\n";} 
}; 

int main() 
{ 
    A* a = new B; 
    a->foo(); 
} 

This works because at compile time the compiler can only see that a is a pointer to the base class A and foo() is a public method being called on a, which is perfectly valid. The virtual binding happens dynamically at run time after the compilation, this virtaul binding decides that the actual call is to B::foo() and not A::foo() that is the the performance penalty of using virtualism.

like image 84
Alok Save Avatar answered Oct 13 '22 00:10

Alok Save


May not answer all your questions directly, nevertheless I decided to put it up here for future reference. Also please take it with a pinch of salt as this is based on my understanding of the events that have happened in the C++ Standard world, rather than the actuals.

Read this. I don't have the ARM with me, but the article gives necessary details.

Note 115 in C++0x says

115) Access declarations are deprecated; member using-declarations (7.3.3) provide a better means of doing the same things. In earlier versions of the C++ language, access declarations were more limited; they were generalized and made equivalent to using-declarations in the interest of simplicity. Programmers are encouraged to use using-declarations, rather than the new capabilities of access declarations, in new code.

In summary:

I think the ARM prohibited it initially:

An access declaration may not be used to restrict access to a member that is accessible in the base class, nor may it be used to enable access to a member that is not accessible in the base class.

But later on I guess when the Standard evolved this was eventually allowed

like image 34
Chubsdad Avatar answered Oct 13 '22 00:10

Chubsdad