Skip to main content
GameDev.net gamedev.net
🔒 Locked

class member functions vs free functions

Started by nuclear123 Nov 7, 2010 at 11:37 AM 3 replies 1.2k views
Original Post
nuclear123
nuclear123
Does anyone happen to have any good advice on how i should be able to decide if a function should be inside of my class or a free function?

i feel like i find it hard to decide what functions should be associated with my class and what shouldn't be!
AngleWyrm
AngleWyrm
If the function might be used elsewhere then it's a candidate for outside of the class. If it's only going to be used within the class, then it's a good choice for a class member.
--"I'm not at home right now, but" = lights on, but no ones home
Zakwayda
Zakwayda
Here is an article that might be of interest.
nuclear123
nuclear123
i read that article and i understand some of it but i still feel like my awnser is not found there. i understand of course the more functions u add/change in a class increase the chance of breaking/rendering the class usesless. But i'm still unsure of how to determine what functions needs to be member functions, and what functions needs to be non member functions.

class Process
{
private:
std::wstring processName;
int processID;
handle processHandle;
public:
Process(const std::wstring processName);
~Process();
isValidProcess(); <--- i was told this should be a nonmember function but i'm unsure why!
};

i feel like if i want this class to be successfull it should have a function in it which can perform error checking so the user can validate whether the process is actually a valid process on the system. What if i release process class that is below which doesn't contain any error checking

class Process
{
private:
std::wstring processName;
int processID;
handle processHandle;
public:
Process(const std::wstring processName);
~Process();
};


my proce
rip-off
rip-off
If isValidProcess() is a free function, how can it determine whether the process is valid?

The rule of thumb is to use free functions when you can, and member functions when you must. Member functions should be those that manage invariants or require access to private data.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.