static class vs this?

Started by
21 comments, last by Hodgman 13 years ago
is there a difference/overhead in doing this....


class Log
{
private: // no members
public:
void createLog( );
};

int main()
{

Log log1;
log1.createLog();

}





class Log
{
private: // no members
public:
static void createLog( );
};


int main()
{
Log::createLog();
}


is this the same exact code? or does the non static one create useless overhead and is bad code/slower
Advertisement
No, they are not the same.

A static member function does not operate on an object.
A non-static member function must have an object, which is automatically passed to the function as the this pointer.

The performance difference between the two is very nearly nothing.


Generally you use a static member function when you want a function to be part of a class interface but you won't actually reference a part of the object. For example, you may use it to look up common values.

I'm guessing that eventually createLog is going to modify the state of a Log object, so it shouldn't be a static function.
This looks eerily like you are going to implement singletons...
If you want Log to be available with the same instance everywhere, think about using a Singleton.


class Log : public boost::noncopyable
{
private:
Log()
{}

public:
static Log& getInstance()
{
static Log OnlyOne;
return OnlyOne;
}

void createLog()
{
// .....
}

void logIt(int, string&)
{}
};

#define LOGERROR(s) Log::getInstance().logIt(L_ERROR, s)

int main()
{
Log::getInstance().createLog();
LOGERROR("I am feeling depressed.");
}


Static Values can also be used to define Strings or things used over the whole program:

class Math
{
static double PI;
};

double Math::PI = 3.14159265;

int main()
{
using namespace std;
cout << "PI is exactly: " << Math::PI << endl;
}
If you say "pls", because it is shorter than "please", I will say "no", because it is shorter than "yes"
http://nightlight2d.de/

If you want Log to be available with the same instance everywhere, think about using a Singleton.

Why not just use a global?
I'd expect a modern compiler to create the same code for both cases.

The only difference is that one is an evil global, and one isn't.
If logging is your bottleneck then you are doing it wrong. In any case with logging the slow part is usually the I/O, not the function calls themselves. Don't micro optimise unless you have a performance problem and you have exhausted macro optimisations.

[quote name='NicoG' timestamp='1301513209' post='4792295']
If you want Log to be available with the same instance everywhere, think about using a Singleton.

Why not just use a global?
[/quote]

Of course could he use a global, but in my eyes a singleton provides a cleaner access-interface.
If you say "pls", because it is shorter than "please", I will say "no", because it is shorter than "yes"
http://nightlight2d.de/
I believe a Singleton provides a worse interface. It tends to be more verbose, and it is harder to create sub-system local log instances. For example, you might want to set the log verbosity of your graphics subsystem to high, while keeping others low. For low complexity games, I'd probably just use simple procedure calls rather than create a class at all. Any state can be kept in an anonymous namespace.

Or just use std::cout, std::cerr and std::clog directly. I might modify their rdbuf() to output to a in-game console or to a file, or I might use OS-level pipes to capture the output to a file.

I believe a Singleton provides a worse interface. It tends to be more verbose, and it is harder to create sub-system local log instances. For example, you might want to set the log verbosity of your graphics subsystem to high, while keeping others low. For low complexity games, I'd probably just use simple procedure calls rather than create a class at all. Any state can be kept in an anonymous namespace.

Or just use std::cout, std::cerr and std::clog directly. I might modify their rdbuf() to output to a in-game console or to a file, or I might use OS-level pipes to capture the output to a file.


When you use a singleton, it is guaranteed that you cannot access the singleton before it is created. Hence getInstance().
You don't have that guarantee with static globals.

I use such a logging singleton for creating html logs. You can represent debugging data much better with that when you are unable to pause the process because it depends on timing etc, which is esp. the case when you are doing games. Well, at least it is an easy method to achieve this.
I made myself a class log and logwriter. logwriter can be derived and output any format such as csv, text or html.
If you say "pls", because it is shorter than "please", I will say "no", because it is shorter than "yes"
http://nightlight2d.de/

This topic is closed to new replies.

Advertisement