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

Quick question regarding member access

P: n/a
Hello.

Given is the following class:

class Test
{
public:
int i;
int& get() { return i; }
};

Now, is the following member access

Test test;
test.i; // this here

equivalent to this method call?:

Test test;
test.get();

Do I have to worry about what is faster? If they are not equal, do
compiler optimize to get rid of this difference in accessing the
member(s)?

Thanks in advance.

Oct 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
no both of them are different. Its compiler's job to decide if it wants
to optimize this or not and it has all the reasons to do this. U can
try suggesting teh compiler by saying making it inline but that's all u
can do...
in nutshell test.get() will can never be faster than test.i.
thanks
ravinder thakur
ma*******@googlemail.com wrote:
Hello.

Given is the following class:

class Test
{
public:
int i;
int& get() { return i; }
};

Now, is the following member access

Test test;
test.i; // this here

equivalent to this method call?:

Test test;
test.get();

Do I have to worry about what is faster? If they are not equal, do
compiler optimize to get rid of this difference in accessing the
member(s)?

Thanks in advance.
Oct 10 '06 #2

P: n/a
Thanks.

It is very important for me to know that, because I am about to write
an application that will execute either test.i or test.get() about a
million times. So I am not sure which to use.

Oct 10 '06 #3

P: n/a
although that might be important for you, making the member variables
public or returning them by reference is never an accepted way of
writing the OO code.

ma*******@googlemail.com wrote:
Thanks.

It is very important for me to know that, because I am about to write
an application that will execute either test.i or test.get() about a
million times. So I am not sure which to use.
Oct 10 '06 #4

P: n/a
ra************@gmail.com wrote: [top-posting corrected]
ma*******@googlemail.com wrote:
>Thanks.

It is very important for me to know that, because I am about to write
an application that will execute either test.i or test.get() about a
million times. So I am not sure which to use.

although that might be important for you, making the member variables
public or returning them by reference is never an accepted way of
writing the OO code.
a) Well, then the code is not OO but fast. That doesn't need to be a bad
thing :-)

Seriously: whether public member variables are cool or uncool depends very
much on the context. For instance, even in OO design there are cases where
the following is perfectly fine:

class OO_Thing {
private:

struct Internal_Data {

// lots of public members

}; // Internal_Data

public:

// some stuff not exposing Internal_Data at all

}; // OO_Thing
b) Please do not top-post. It is considered poor form in this news group.
Best

Kai-Uwe Bux
Oct 10 '06 #5

P: n/a
ma*******@googlemail.com wrote:
Thanks.

It is very important for me to know that, because I am about to
write
an application that will execute either test.i or test.get() about a
million times. So I am not sure which to use.
You should use the one that makes your code easier to understand.

If you have a compiler that cannot optimize your get() function, you
are in a lot of trouble elsewhere!

Also, on a 3 GHz machine, one million accesses take no time flat -
whatever you do. You have *billions* of instructions per second to
play with!

Bo Persson
Oct 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.