Whats the syntax for getPrivateMember

Started by
7 comments, last by executor_2k2 22 years, 2 months ago
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
Well, that was a waste of 2 minutes of my life. Now I have to code faster to get 'em back...
Advertisement
Try this ...
int getNumParticles() {return m_numParticles;}
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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 ]
Their called methods.
Oh, and the kind you''re talking about are called accessor methods or accessor functions.
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
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
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.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Perhaps this would be a bit better:
int getNumParticles() const {return m_numParticles;}
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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 ]

This topic is closed to new replies.

Advertisement