By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

accessing a private member (illegally)

P: n/a
Tom
Let's say I've got a class defined in a header file.

class foo {
private:
...
int bar;
....
};

Now lets say I have a function that needs to access the private variable
bar:

void function() {
....
foo x;
x.bar = 5; // illegal, bar is private
}

Of course, the normal way to do this is with a friend declaration. But
lets say I have a constraint where I cannot modify the header file.
Basically, I don't want to change anything inside the class foo {...}
declaration. Is there any other way to get access to x.bar from
function()?

Things that come to mind:

1) make the friend declaration outside the header file. It would be
nice if something like this worked:
foo::friend void main();
But I can't find any syntax that accomplishes this, so I'm thinking it
isn't possible.

2) "cast" the member variable to remove it's private modifier. I don't
think C++ allows this either.

3) access bar through low-level pointer hacks. This is possible, but
compiler dependant and non portable.

Are there any other options? I've got my shotgun loaded and pointing in
the vicinity of my foot, now I just need to figure out how to remove the
safety lock. :)

Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Tom wrote:
Let's say I've got a class defined in a header file.

class foo {
private:
...
int bar;
....
};

Now lets say I have a function that needs to access the private variable
bar:

void function() {
...
foo x;
x.bar = 5; // illegal, bar is private
}

Of course, the normal way to do this is with a friend declaration. But
lets say I have a constraint where I cannot modify the header file.
Basically, I don't want to change anything inside the class foo {...}
declaration. Is there any other way to get access to x.bar from
function()?

Things that come to mind:

1) make the friend declaration outside the header file. It would be
nice if something like this worked:
foo::friend void main();
But I can't find any syntax that accomplishes this, so I'm thinking it
isn't possible.
It's not possible because it defeats the point of privacy and friendship.
When I inspect the header file X.h, I only need to read the file to know
what's private, what's restricted, and what's public.
2) "cast" the member variable to remove it's private modifier. I don't
think C++ allows this either.
Privacy is not a type or qualification, it is an access.
3) access bar through low-level pointer hacks. This is possible, but
compiler dependant and non portable.
They are portable when they use offsetof(). It portably returns the offset
of a member; you typecast the address of an object to
Are there any other options? I've got my shotgun loaded and pointing in
the vicinity of my foot, now I just need to figure out how to remove the
safety lock. :)


The other option is to modify that header file, or use the interface of its
constituents the way they were designed to be used.

You could also write this above the header:

#define private public

The C++ Standard undefines the act of re-defining a keyword above the
#include for a Standard header file. It also undefines the result of linking
two translation units that disagree on the exact definition of a class they
share (one definition rule). But nobody checks these violations...

However, in this case, the bullet /might/ go thru your foot with only minor
soft-tissue damage, so try it!

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 22 '05 #2

P: n/a
Tom
"Phlip" <ph*******@yahoo.com> wrote in
news:qc*****************@newssvr32.news.prodigy.co m:

BTW, before I go on, I should mention that the application underlying
this question is an object serialization framework I am developing --
one of the canonical examples of low-level functionality when access
qualifiers just get in the way.
They are portable when they use offsetof(). It portably returns the
offset of a member; you typecast the address of an object to
Good idea, but it sadly it doesn't work. At least not with g++. I
still get an error that the member variable is private. Looking at the
definition in stddef.h I'm not surprised. You can't take an address of
a private member.

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
The other option is to modify that header file
Not an option for two reasons. It's a general framework, it would be
prohibitive to modify every header file its ever applied to. Also, it
could break binary code compiled with the original headers, as you noted
below, unless the change has absolutely no effect on the binary
structure.
You could also write this above the header:

#define private public
Now this is promising. :) In fact I could use a slight variation:
#define private friend void function(); private

This almost works, but it will fail if the class defines private
variable implicitly at the top of the class without ever using the
private keyword. But maybe that could be fixed with:
#define class struct. (hehe...)
I would also need to verify that this change doesn't change the binary
structure on compilers I care about.
However, in this case, the bullet /might/ go thru your foot with only
minor soft-tissue damage, so try it!


Thanks for your suggestions, I'm getting closer. I'm going to try
experimenting with the preprocessor hacks and testing if it's good
enough to get the job done.

It would be great if the addressof operator worked, because that would
be a perfect solution.
Jul 22 '05 #3

P: n/a
Phlip wrote:

Tom wrote:
Let's say I've got a class defined in a header file.

class foo {
private:
...
int bar;
....
};
[...]
3) access bar through low-level pointer hacks. This is possible, but
compiler dependant and non portable.


They are portable when they use offsetof(). It portably returns the offset
of a member; you typecast the address of an object to


Since offsetof is only defined for PODs, it cannot be used legally (or
portably for that matter, if only because many compilers would bulk at such
attempt) for any private or protected data members.

Denis
Jul 22 '05 #4

P: n/a
Tom <t@h.c> wrote:
void function() {
...
foo x;
x.bar = 5; // illegal, bar is private
}

Of course, the normal way to do this is with a friend declaration. But
lets say I have a constraint where I cannot modify the header file.
Basically, I don't want to change anything inside the class foo {...}
declaration. Is there any other way to get access to x.bar from
function()?


You may find this useful: http://www.gotw.ca/gotw/076.htm
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.