with get functions, use pointer or declare function as?

Started by
12 comments, last by johnnyBravo 19 years, 12 months ago
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]
Advertisement
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]
Maciej Sawitus
my blog | my games
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.
Lol!
Maciej Sawitus
my blog | my games
Or you could pass the pointer by reference.
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?
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.
ok for things larger then 32bits i use pointers
and for things equal or less to 32bits i use returns.

Just totally making sure
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]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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,

This topic is closed to new replies.

Advertisement