Archived

This topic is now archived and is closed to further replies.

johnnyBravo

with get functions, use pointer or declare function as?

Recommended Posts

Hi, with my 'get' functions eg:
quote:
int getNumber() { return number; }
...should I put the function like the one above, or should I do it like this:
quote:
void getNumber(int * returnNumber) { returnNumber= number; }
Like my get functions would include things like LONGLONG, string, int, float, double, long. Most of the microsft dx functions use the pointer way,... whats the difference. Like memory wise or something. I actually prefer using ones where they return a value so i don't have to create a variable for the value to be entered in. Thanks, [edited by - johnnyBravo on April 21, 2004 4:43:24 AM]

Share this post


Link to post
Share on other sites
Hi!

The second one will not work, maybe it's a typo, but should rather be [Edit: corrected] "*returnNumber = number".

The general answer to your question is that it depends on what you want.

In your first example you make a copy of an object (here it's just int, well ints are not objects in C++, but its same with objects) thus for something bigger that int it might cost a lot (speed and memory).

In the second example you return an address of an object allowing this way to change it from the outside of your class, which probably is not what you'd expect (why to make accessors then?).

A common way for simple and convenient accessors is through reference with optional const in "[]" brackets if you wish it wasn't changed outside:

inline [const] SomeType& attribute() const
{
return Attribute;
}

Hope it helps.

[edited by - MickeyMouse on April 21, 2004 6:10:38 AM]

Share this post


Link to post
Share on other sites
MickeyMouse what you suggested won''t work either.
Should be: *returnNumber = number;

Other than that, what MickeyMouse said was correct. For example if you have an class with an expensive copy constructor or a class that is quite big, the second way might be faster.

Share this post


Link to post
Share on other sites
quote:
Original post by Lantz
MickeyMouse what you suggested won''t work either.
Should be: *returnNumber = number;




huh, i just made my get functions and they work with what micky said.

Ill try the *return... = num...

So for my gets involving int, long, char,float should i use the pointers for them aswell?

Share this post


Link to post
Share on other sites
quote:
Original post by johnnyBravo
So for my gets involving int, long, char,float should i use the pointers for them aswell?

Nope that won''t do any good. It will just make your code look crappy. Also if you use return the compiler should return it through a register if what you''re returning is 32 bits or less which is by far faster than accessing the memory. It probably won''t be able to do this if you''re using the pointer method.

Share this post


Link to post
Share on other sites
quote:
Original post by johnnyBravo
Most of the microsft dx functions use the pointer way,... whats the difference. Like memory wise or something.


The Microsoft ones work like that because DX is based on COM. In COM, the return value is reserved for an error code, the HRESULT.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
Ok this line is from the play3Dsound sample in dx90sdk. From the CSoundManager class.
quote:
inline LPDIRECTSOUND8 GetDirectSound() { return m_pDS; }


Is there a reason why this would be ok, or not?

thanks,

Share this post


Link to post
Share on other sites
quote:
Original post by ChaosEngine
a simple rule I follow for accessors is for pods return by value for udts return by const reference


sorry could you put that into simpler terms?

Share this post


Link to post
Share on other sites
quote:
Original post by johnnyBravo
quote:
Original post by ChaosEngine
a simple rule I follow for accessors is for pods return by value for udts return by const reference


sorry could you put that into simpler terms?


if return is not class thingo I no use & on type. If I define a clas thing I use squigily and sign thing

Share this post


Link to post
Share on other sites
If you''re returning an objects, return by const reference. For plain old data types, return by value.

std::string const& GetName();
int GetID();


quote:
Original post by ChaosEngine
quote:
Original post by johnnyBravo
quote:
Original post by ChaosEngine
a simple rule I follow for accessors is for pods return by value for udts return by const reference


sorry could you put that into simpler terms?


if return is not class thingo I no use & on type. If I define a clas thing I use squigily and sign thing


Could you define the terms "squigily" and "thingo"? Thank you.

Share this post


Link to post
Share on other sites