class member access specifiers and binary code

I understand what the typical access specifiers are, and what they mean. 'public' members are accessible anywhere, 'private' members are accessible only by the same class and friends, etc.

What I'm wondering is what, if anything, this equates to in lower-level terms. Are their any post-compilation functional differences between these beyond the high-level restrictions (what can access what) imposed by the language (c++ in this case) they're used in.

Another way to put it - if this were a perfect world where programmers always made good choices (like not accessing members that may change later and using only well defined members that should stay the same between implementations), would their be any reason to use these things?

-------------Problems Reply------------

Access specifiers only exist for compilation purposes. Any memory within your program's allocation can be accessed by any part of the executable; there is no public/private concept at runtime

Michael's answer is right. Access specifiers do not directly affect the resulting code.

However, access specifiers may resolve ambiguous identifier/overload errors that would otherwise prevent compilation.

class A {
int x;

class B {
int x;

class C : public A, public B {
int &get_x() { return x; } // only B::x is accessible, no error.

So they definitely serve a higher purpose than restricting the programmer.

The answer to your question could differ depending on the compiler, but in general there will be no difference. One could conceive though of an environment for which the compiled code might have different characteristics for those different accessibilities, but I'm not aware of any that exist.

Programmers can only make good choices when armed with the right information. Access modifiers are a way to signal to the programmer that certain things shouldn't be touched, and it has the side-benefit of enforcing correct behaviour.

There is no runtime impact. You could write a program with correct access modifiers, build it with, c++ -Dprotected=public -Dprivate=public, and it should build and produce almost exactly the same code (There are some hypothetical caveats such as data layout of classes).

Post-compilation, you are left with machine code (assembly) which has no notion of "public" or "private" (or of classes, members, etc). Everything is simply a memory address (whether it's code or data), and can be accessed just like any other memory address. The whole public\private distinction (as well as almost every other construct available in a high-level language) is purely for the benefit of the programmer, allowing the compiler to enforce a set of rules that are intended to make the intent of the code clearer and to help avoid potential bugs. Once compiled, your code doesn't know what language it was originally written in, much less what type of access specifiers were used.

That being said, it would be possible to rig a compiler so that it modifies the code whenever a private class member function is called in order to detect when the function is called inappropriately (add an extra parameter and set it to some expected value when the function is called from within the class; calling the function from outside of the class would provide the wrong value). The problem with this approach is what do you do now? Lock up? Do nothing and return invalid data? These types of problems are (relatively) easily detectable and correctable at compile time, so it is rare to see this sort of thing enforced at run time (outside of debugging or code profiling tools).

Category:c# Views:0 Time:2010-05-03

Related post

  • Ambiguous member access expression: is Clang rejecting valid code? 2011-11-11

    I have some code that, for the purposes of this question, boils down to template<typename T> class TemplateClass : public T { public: void method() {} template<typename U> static void static_method(U u) { u.TemplateClass::method(); } }; c

  • Private Member Access Java 2009-03-24

    Is the private member access at the class level or at the object level. If it is at the object level, then the following code should not compile class PrivateMember { private int i; public PrivateMember() { i = 2; } public void printI() { System.out.

  • LinqToSQl and the Member access not legal on type exception 2009-02-07

    The basic problem... I have a method which executes the following code: IList<Gig> gigs = GetGigs().WithArtist(artistId).ToList(); The GetGigs() method gets Gigs from my database via LinqToSql... So, when GetGigs().WithArtist(artistId).ToList()

  • C pointers vs direct member access for structs 2009-08-25

    Say I have a struct like the following ... typedef struct { int WheelCount; double MaxSpeed; } Vehicle; ... and I have a global variable of this type (I'm well aware of the pitfalls of globals, this is for an embedded system, which I didn't design, a

  • Member access differences 2009-12-28

    can someone tell me what is the different between (*ptr).field and ptr->field? I know it connect somehow to static and dynamic linking, but i dont know what is it. can someone tell me the differnet and give me an example? edit: if i have this code

  • What does "Member access 'DataType MemberName' of 'Namespace.ClassName' not legal on type 'System.Collections.Generic.IEnumerable`1[Namespace.ClassName]." mean? 2010-02-17

    I would love a solution to my current problem, but I would love EVEN MORE if I can understand what that error actually means. I have LINQ to SQL classes for two tables in my DB: Providers and Assignments. I added the following member to the Provider

  • Member access syntaxes for pointer to a structure 2010-06-04

    #include <iostream> typedef struct _person { std::string name; unsigned int age; }Person; int main() { Person *pMe = new Person; pMe->age = 10; pMe->name = "Larson"; std::cout << "Me " << (*pMe).age << " " << (*pMe

  • Using a member access lambda expression to parametrise a LINQ to SQL predicate 2010-08-09

    I have a query that needs to be reused all over the place and I need to vary which property/column gets used for a join. What I'd like to be able to do is something like: query = RestrictByProp(query, x=>x.ID); An extremely simplified RestrictByPr

  • A property, indexer or dynamic member access may not be passed as an out or ref parameter 2011-01-27

    This question already has an answer here: C# property and ref parameter, why no sugar? 9 answers A property or indexer may not be passed as an out or ref parameter 6 answers Hello I'm having trouble figuring this out. I have these structs and classes

  • Gray Code to Binary Code Conversion 2011-03-15

    So I'm trying to write a function that will convert a 7-bit Gray code to the corresponding 7-bit Binary Code. Here's how to convert - Gray Value bits ---- MS bit > (G6) G5 G4 G3 G2 G1 G0 * Binary Value bits -- MS bit > (B6) B5 B4 B3 B2 B1 B0 *

  • How do get binary code from png on uses javascript 2011-08-11

    How do get binary code from png on uses javascript? Is there methods for getting binary code? --------------Solutions------------- If you mean the actual PNG encoded data, then, no, it's not possible. But of you want to access the pixel data, you can

  • Overloading member access operators ->, .* (C++) 2012-01-08

    I understand most operator overloading, with the exception of the member access operators ->, .*, ->* etc. In particular, what is passed to these operator functions, and what should be returned? How does the operator function (e.g. operator-

  • PHP Class member access on instantiation 2012-03-04

    I have a problem with the new syntax of PHP 5.4 My code with member access on instantiation $oClass = (new Foo)->bar(); $oClass->bar2(); I get this error Fatal error: Call to a member function bar2() on a non-objec Why? EDIT: I added return $th

  • what data structure should I use to store binary codes in java? 2012-04-04

    I have binary codes as 10111 , 100011, 11101111 etc. now what data structure should I use to store these codes so that minimum size is required to store them? I can't use string array as size required will be more as compared than storing the decimal

  • How to disable public access to the source code in Google Code? 2009-06-21

    I have a private project, and i want it hosted on google code. this project is just for project members. How to enable browsing and submitting SVN code only for project members? I don't want other people to be able to do this. How to disable public a

  • How can I access x:Name from Code Behind? 2009-11-18

    I have a datagrid in which I am using DataGridTemplateColumn and DataGridTextColumn. I want to access these columns at runtime so I have assigned x:Name property to them. But I was not getting that value in code behind, so I looked for DataGrid and t

  • Safari displaying the binary code instead of downloading Adobe air application 2009-11-23

    I'm using Adobe Air 1.5 with Safari 4.0.3 on Mac OS Snow Leopard. When the browser attempts to download the Air file, binary code is displayed in the browser instead of downloading the Air application. Firefox has no problems downloading this Air app

  • VB.NET: Doesn't anyone use the dictionary member access expression? (a.k.a. the bang operator) 2009-12-04

    Doesn't anyone use the dictionary member access expression? (a.k.a. the bang operator) If so, in what scenarios? --------------Solutions------------- This construction is used in custom

  • reading binary code of a file-in PHP 2010-02-04

    How can I read the binary code(to get the 1s and 0s) of a file. $filename = "something.mp3"; $handle = fopen($filename, "rb"); $contents = fread($handle, filesize($filename)); fclose($handle); I tried this but it shows some strange characters... i pr

Copyright (C), All Rights Reserved.

processed in 0.095 (s). 11 q(s)