C++ class: to static or not

N

neelsmail

Hi,

While adding a class, I usually have question: to make it static or
not. Here is an example:

class myclass {
SomeType someTypeMember;
public:
SomeOtherType DoSomething() const {
//do something with someTypeMember and return SomeOtherType
return someOtherType;
}
};

The same class can be written as (the method I don't prefer):

class myclass {
public:
static SomeOtherType DoSomething(SomeType someTypeVar) {
//do something with someTypeVar and return SomeOtherType
return someOtherType;
}
};

Is there a recommended reason why one style should be preferred than
other?

Thanks in advance,
-Neel.
 
I

Ian Collins

Hi,

While adding a class, I usually have question: to make it static or
not. Here is an example:

class myclass {
SomeType someTypeMember;
public:
SomeOtherType DoSomething() const {
//do something with someTypeMember and return SomeOtherType
return someOtherType;
}
};

The same class can be written as (the method I don't prefer):

class myclass {
public:
static SomeOtherType DoSomething(SomeType someTypeVar) {
//do something with someTypeVar and return SomeOtherType
return someOtherType;
}
};
White space is free these days...
Is there a recommended reason why one style should be preferred than
other?
If function operates on the class rather than an instance of the class
it should be static.

If the member function is not static, an instance of the class is
required in order to call it.

Static members make the design clear and avoid the overhead of the
unused this pointer.
 
N

neelsmail

White space is free these days...


If function operates on the class rather than an instance of the class
it should be static.

If the member function is not static, an instance of the class is
required in order to call it.

Static members make the design clear and avoid the overhead of the
unused this pointer.

Thanks for the replies. This is a way I look at it:

- Yes, class is a type with associated actions.
- But, I look at it as composite data type (if there is such a term).
- In the example given above, you can consider it a data type. With
more number of members but few/just one action, it becomes a very nice
way to bind it together.
- When you are using this class you can use it:
const SomeClass someObject(someType);

This way it becomes a very good place to keep related things together
(even if it doesn't have an action associtated with it but generally
it does).

- This is the same reason why I don't prefer static because it looks
as if it's a replacement for global functions than a class.
- The only thing that gets in the way is if it isn't static there has
to be a object created before we can use that class. But, given the
fact that if you have static it will always live, I like to remedy the
object creation problem with either singleton or simply by calling:

someOtherType = SomeClass(someType).SomeMethod();

Does this makes sense?

Thanks,
-Neel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top