You are here:

C++/about class access modifiers ?

Advertisement


Question
Hi,

The question you left me as follows,

----------------------------------
You might also wish to consider how the application of such per-object access rules would apply to static members - or class members in more general OO terms - as they are not associated with any particular object how should the access specifiers apply to them?
-------------------------------

Since access modifiers are class level and static members belong to classes, I think there are no differences when access modifiers apply to static and instance members. Is that right?

Thanks,
lzzzz




-------------------------
Followup To
Question -
Hi,

Somebody told me that the access modifiers is class level, not object level in java, How about in C++? and I was wondering why it is class level, not object level?

Thanks,
lzzzz
Answer -
Well, if I understand what you mean, I think you will find C++ is the same as Java.

If the access specifiers were applied at the object level then each instance of a class could not access private (or presumably protected) members of other instances of the same class. This would force everything to be public - otherwise you could not write operations such as copy constructors, for example:

class C
{
public:
       C( C const & other )
       {
         iSzPtr = new char[ strlen(other.iSzPtr)+1 ]
         strcpy( iSzPtr, other.iSzPtr );
       }
// ...

private:
       char * iSzPtr;
};

Note that this is not a complete example and I choose a case where you would require an explicit copy constructor rather than relying on the one the compiler would generate for you. In real life I probably would use a std::string instead of a C style zero terminated char array string and no copy constructor would be required. However I would expect the problem just to move into the implementation of the std::basic_string class template - the basis for std::string and std::wstring. Also compiler generated copy constructors should be subject to the same access rules.

Now back to the point: If access were applied on an object by object basis then the above code would not compile as the this-object would not be able to access the other-object's iSzPtr private member in the copy constructor. Hence the only way to allow such access would be to make iSzPtr public. So by tightening up the access rules by making the access specifiers apply on a per-object basis rather than on the type of the object you would in fact force a more lax usage style - which would make the access specifications protected and private pretty useless.

What about base sub-objects that are not publicly inherited? Would access to them be considered access to a different object? Probably not so here, is a possible area in which protected and private could still be of use if access were applied on an object-by-object basis. This seems ok until once again we come to copy construction. Here the protected or private base sub-object has to be initialised from the members of some other object's protected or private base - to which it would not have access!

You might also wish to consider how the application of such per-object access rules would apply to static members - or class members in more general OO terms - as they are not associated with any particular object how should the access specifiers apply to them?  

Answer
Yes. That's what I rely on static member functions for example have access to private and protected members of the class they are members of you just have to remember to obtain an instance from somewhere if you wish to access any instance members:

       class S
       {
       public:
         static void SMF( S & s );
 
       private:
         int        iN;
       };

       void S::SMF( S & s )
       {
         s.iN = 23;
       }

If this were not so why bother associating them with a class at all you might just as well make them ordinary non-member functions otherwise.

My point was if we assume the other option, i.e. access specifiers are applied at the object level rather than the class level, what would this then mean for static members?  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.