Archived

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

executor_2k2

Whats the syntax for getPrivateMember

Recommended Posts

executor_2k2    122
Whats the syntax for a function defined in a class''s public area that return the private member variable all in one line? I think its something like class woho { public: int getNumParticles(); {return m_numParticles} the above doesnt work but i know it look something like that. Can anyone help out? Thanks

Share this post


Link to post
Share on other sites
Kylotan    9879
You''re making it out to be some sort of special construct: it is just a function like any other. It is just a function which returns a value (that just happens to be a private member.) So the return statement needs a semicolon like any nornal statement.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Oh, and the kind you''re talking about are called accessor methods or accessor functions.

Share this post


Link to post
Share on other sites
ChaosEngine    5185
if you want it really correct and fast us
  
class woho
{
public:
int getNumParticles() {return m_numParticles; } const;
private:
int m_numParticles;
};


or if you don't want to expose your implementation in your interface (generally a good idea) you can write a inline file like this...
class.h
  
class woho
{
public:
inline int getNumParticles() const;
private:
int m_numParticles;
};

#include "class.ipp"

class.ipp
  
inline int woho::getNumParticles() const;
{
return m_numParticles;
}


"That's not a bug, it's a feature!"
--me

Edited by - ChaosEngine on February 1, 2002 3:35:54 AM

Share this post


Link to post
Share on other sites
NuffSaid    122
The code posted by Chaos Engine will compile under MSVC (don''t know about other compilers).
For an inline function, the definition of the function has to be in the same header file as its declaration. Else if won''t compile.

Share this post


Link to post
Share on other sites
Kylotan    9879
quote:
Original post by NuffSaid
For an inline function, the definition of the function has to be in the same header file as its declaration. Else if won''t compile.

No, the definition just needs to have been included (directly or indirectly) by every file which calls that inline function.



[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites