473,322 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Access to member variable by 'friend'

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
Jul 22 '05 #1
5 2153
Morten Aune Lyrstad wrote:
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

Jul 22 '05 #2
Krishanu Debnath wrote:
Morten Aune Lyrstad wrote:

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

Won't you still get function call overhead, or will the compiler
understand what I want and make a simple assignment?
Jul 22 '05 #3
Morten Aune Lyrstad wrote:

Krishanu Debnath wrote:
Morten Aune Lyrstad wrote:

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

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
Ok; thank you for clearing that up for me! :-)
Jul 22 '05 #5
Morten Aune Lyrstad wrote:
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.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
5
by: JustSomeGuy | last post by:
I have a class with a private data member and I want to access it from a friend function, but Visual Studio 2003 .NET won't let me. class MyClass { private: static int level; public: friend...
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: Samuel Burri | last post by:
Hi there I got a simple problem. As you can see in the posted source, I have two classes, where one is derived from the other. In fact, they also contain virtual functions. In the function...
2
by: dobest03 | last post by:
Hi. Are there any way to access the integer member 'a' of outer structure from inner structure's member function func_inner()? See below the structure... Thanks. struct outer {
2
by: cdg | last post by:
Is there any way to directly access variables of one class from another class without passing any public member variables. Similar to how any function in a class can directly access (without...
1
by: Immortal_Nephi | last post by:
I want to show you an example how two classes can have relationship instead of an inheritance. The inheritance is not an option if you want to define two classes. The relationship between two...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.