Connecting Tech Pros Worldwide Forums | Help | Site Map

Access to member variable by 'friend'

Morten Aune Lyrstad
Guest
 
Posts: n/a
#1: Jul 23 '05
I am in the process of writing a virtual machine. I know that one should
not give access to private variables in a class (by using friend) but
wouldn't that speed up execution? Like this:

class Script;

class Instruction
{
...
private
int someCode;
friend Script;
}

class Script
{
...
void Execute(...)
{
switch (currentScript.someCode) {
... ... ...


rather than using a function by the name GetCode or whatever? Or is
there some other reason why this should not be done? As long as the user
of these scripts don't get access to those variables, everything should
be ok, right?

Yours,
Morten Aune Lyrstad

Krishanu Debnath
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Access to member variable by 'friend'


Morten Aune Lyrstad wrote:
[color=blue]
> I am in the process of writing a virtual machine. I know that one
> should not give access to private variables in a class (by using
> friend) but wouldn't that speed up execution? Like this:
>[/color]
Why don't you use inline member_access function?

Krishanu

Morten Aune Lyrstad
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Access to member variable by 'friend'


Krishanu Debnath wrote:[color=blue]
> Morten Aune Lyrstad wrote:
>
>[color=green]
>>I am in the process of writing a virtual machine. I know that one
>>should not give access to private variables in a class (by using
>>friend) but wouldn't that speed up execution? Like this:
>>[/color]
>
> Why don't you use inline member_access function?
>
> Krishanu
>[/color]
Won't you still get function call overhead, or will the compiler
understand what I want and make a simple assignment?
Karl Heinz Buchegger
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Access to member variable by 'friend'


Morten Aune Lyrstad wrote:[color=blue]
>
> Krishanu Debnath wrote:[color=green]
> > Morten Aune Lyrstad wrote:
> >
> >[color=darkred]
> >>I am in the process of writing a virtual machine. I know that one
> >>should not give access to private variables in a class (by using
> >>friend) but wouldn't that speed up execution? Like this:
> >>[/color]
> >
> > Why don't you use inline member_access function?
> >
> > Krishanu
> >[/color]
> Won't you still get function call overhead, or will the compiler
> understand what I want and make a simple assignment?[/color]

You worry to much.
Exactly that was the intent in 'inline' functions: To get rid of
the call overhead.
While the compiler is free to ignore your hint, it is unlikely
that it does so in such simple cases: Such a compiler would not
sell or not be used in practice.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Morten Aune Lyrstad
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Access to member variable by 'friend'


Ok; thank you for clearing that up for me! :-)
Micah Cowan
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Access to member variable by 'friend'


Morten Aune Lyrstad wrote:
[color=blue]
> I am in the process of writing a virtual machine. I know that one should
> not give access to private variables in a class (by using friend) but
> wouldn't that speed up execution? Like this:
>
> class Script;
>
> class Instruction
> {
> ...
> private
> int someCode;
> friend Script;
> }
>
> class Script
> {
> ...
> void Execute(...)
> {
> switch (currentScript.someCode) {
> ... ... ...
>
>
> rather than using a function by the name GetCode or whatever? Or is
> there some other reason why this should not be done? As long as the user
> of these scripts don't get access to those variables, everything should
> be ok, right?[/color]

A function such as GetCode() could easily be written as an inline
function member, in which case speed would not be an issue, on
any decent implementation.
Closed Thread