Connecting Tech Pros Worldwide Help | Site Map

accessing a private member (illegally)

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 05:58 PM
Tom
Guest
 
Posts: n/a
Default accessing a private member (illegally)

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. :)


  #2  
Old July 22nd, 2005, 05:58 PM
Phlip
Guest
 
Posts: n/a
Default Re: accessing a private member (illegally)

Tom wrote:
[color=blue]
> 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.[/color]

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.
[color=blue]
> 2) "cast" the member variable to remove it's private modifier. I don't
> think C++ allows this either.[/color]

Privacy is not a type or qualification, it is an access.
[color=blue]
> 3) access bar through low-level pointer hacks. This is possible, but
> compiler dependant and non portable.[/color]

They are portable when they use offsetof(). It portably returns the offset
of a member; you typecast the address of an object to
[color=blue]
> 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. :)[/color]

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



  #3  
Old July 22nd, 2005, 05:58 PM
Tom
Guest
 
Posts: n/a
Default Re: accessing a private member (illegally)

"Phlip" <phlip_cpp@yahoo.com> wrote in
news:qcgUc.1752$DM1.1643@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.
[color=blue]
> They are portable when they use offsetof(). It portably returns the
> offset of a member; you typecast the address of an object to[/color]

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)
[color=blue]
> The other option is to modify that header file[/color]

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.
[color=blue]
> You could also write this above the header:
>
> #define private public[/color]

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.
[color=blue]
> However, in this case, the bullet /might/ go thru your foot with only
> minor soft-tissue damage, so try it![/color]

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.
  #4  
Old July 22nd, 2005, 05:58 PM
Denis Remezov
Guest
 
Posts: n/a
Default Re: accessing a private member (illegally)

Phlip wrote:[color=blue]
>
> Tom wrote:
>[color=green]
> > Let's say I've got a class defined in a header file.
> >
> > class foo {
> > private:
> > ...
> > int bar;
> > ....
> > };
> >[/color][/color]
[...][color=blue]
>[color=green]
> > 3) access bar through low-level pointer hacks. This is possible, but
> > compiler dependant and non portable.[/color]
>
> They are portable when they use offsetof(). It portably returns the offset
> of a member; you typecast the address of an object to
>[/color]

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
  #5  
Old July 22nd, 2005, 06:00 PM
Old Wolf
Guest
 
Posts: n/a
Default Re: accessing a private member (illegally)

Tom <t@h.c> wrote:
[color=blue]
> 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()?[/color]

You may find this useful: http://www.gotw.ca/gotw/076.htm
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.