[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

just a thought about "doing the compilers job"

.rhavin grobert

10/27/2008 2:51:00 PM

guess you have the following:

class X {
int fn1(); // -\ lets assume those fn's
int fn2(); // >- do something independent
int fn3(); // -/ from each other
}

class Y {
public:
X& X() {return m_x;};
private:
X m_x;
};

class Z {
public:
Y& Y() {return m_y;};
private:
Y m_y;
};

_______________________________

now when i do something like this:

Z z;
CallWhateverFunctionTakingThreeInts(z.y().x().fn1,z.y().x().fn2,z.y().x().fn3);

is it as effective as writing ...

Z z;
X& x = z.y().x();
CallWhateverFunctionTakingThreeInts(x.fn1,x.fn2,x.fn3);


1 Answer

Victor Bazarov

10/27/2008 3:00:00 PM

0

..rhavin grobert wrote:
> guess you have the following:
>
> class X {
> int fn1(); // -\ lets assume those fn's
> int fn2(); // >- do something independent
> int fn3(); // -/ from each other
> }
>
> class Y {
> public:
> X& X() {return m_x;};
> private:
> X m_x;
> };
>
> class Z {
> public:
> Y& Y() {return m_y;};
> private:
> Y m_y;
> };
>
> _______________________________
>
> now when i do something like this:
>
> Z z;
> CallWhateverFunctionTakingThreeInts(z.y().x().fn1,z.y().x().fn2,z.y().x().fn3);
>
> is it as effective as writing ...
>
> Z z;
> X& x = z.y().x();
> CallWhateverFunctionTakingThreeInts(x.fn1,x.fn2,x.fn3);
>
>

Generally speaking, the compiler is usually smart enough to take
advantage of inlining of your functions and also to cache the common
subexpression, so, yes it would be as effective in terms of the program
performance (if it isn't, you probably want to chuck that compiler and
get a better one). However, from the readability point of view, the
latter version is waaaaay better.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask