Gamasutra: The Art & Business of Making Gamesspacer
View All     RSS
October 9, 2015
arrowPress Releases
October 9, 2015
PR Newswire
View All
View All     Submit Event

If you enjoy reading this site, you might also want to check out these UBM Tech sites:

Accessing privates without 'friend'(c++)
by Ofer Rubinstein on 01/05/10 04:14:00 pm   Featured Blogs

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.


[ From Pompi Dev Blog

Here is a neat little way I found to make one class access the privates of another class, without using friend or direct inheritance. Take a look at this code:

class A {
        class Observer {
                 int GetPrivate(A & a) {return a.ValueA;};
        int ValueA;
class B: public A::Observer {
        void Foo (A & a) {
             Do something with GetPrivate(a);


Class B cannot access the privates of class A directly, but it inherits a subclass of A that can access the privates of A given as a parameter. Since I have just tried this "pattern" recently, I am not sure what is the direct benefit of this.

I believe a benefit can be in the sense of making a programmer's life easier. Programming languages play several parts. They provide us powerful functionality, but they also suppose to give us ease of use. You can program anything on assembly, but it would be really difficult to write a big game in assembly. If you do not expose privates via a getter, you give the programmer one less method to think about when he need to use the class's instance for something.

Can you think how you would use this "pattern"? Do you think there is no use for it? Tell me what you think.


Related Jobs

Cignition — Palo Alto, California, United States

Server Engineer
Indiana University Media School
Indiana University Media School — Bloomington, Indiana, United States

Game Art
Boss Key Productions Inc
Boss Key Productions Inc — Raleigh, North Carolina, United States

Senior Network Engineer
Titan IM
Titan IM — Anna Bay, New South Wales, Australia

Lead Architect/Senior Developers

Loading Comments

loader image