954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Should you create a class object for each member function of a class?

I am writing some code just for fun and I have a question about class objects. Lets say I have

class Alpha

with member functions

getBeta
getCharlie
getDelta

and so on

now inside main when I want to make calls to these functions is it necessary to create an object to call each individual function such as

Alpha b

b.Beta

Alpha c

c.Charlie

Alpha d

d.Delta

I know this is acceptable but I was wondering if doing this has an advantages or disadvantages over just creating one object to call all class functions like

Alpha a

a.Beta
a.Charlie
a.Delta


any explanation on this will be greatly appreciated. If you don't understand my question I will provide a more concrete example of what I am trying to ask

buddha527
Newbie Poster
21 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Your last statement is how its normally done. Create one object then call all its methods as necessary. If you declare the methods static then you don't have to create an instance of the object in order to call the function

class A
{
public:
   static int getBeta();
};

int main()
{
    A::getBeta();
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Ok thank you AncientDragon

buddha527
Newbie Poster
21 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You