Access to member variable by 'friend'

  • Thread starter Morten Aune Lyrstad
  • Start date
M

Morten Aune Lyrstad

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
 
K

Krishanu Debnath

Morten said:
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:
Why don't you use inline member_access function?

Krishanu
 
M

Morten Aune Lyrstad

Krishanu said:
Morten Aune Lyrstad wrote:



Why don't you use inline member_access function?

Krishanu
Won't you still get function call overhead, or will the compiler
understand what I want and make a simple assignment?
 
K

Karl Heinz Buchegger

Morten said:
Won't you still get function call overhead, or will the compiler
understand what I want and make a simple assignment?

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.
 
M

Micah Cowan

Morten said:
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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top