How is it that when you include cstdlib you can just call methods like atoi without instantiating any object or even prefixing the call with a class name?
I have been making my own string utility library that contains useful string manipulation methods that I haven't found elsewhere. Right now I have a header file with one class called StringUtil and I just add string manipulation methods to this class as I need them.
This works, but it doesn't seem like a good solution because the class doesn't keep any state at all so I'd like to get rid of it and call my string util methods the same way I call atoi - by simply including a header and not making any reference to any class.
I've tried just sticking a bare function in a header file and including the header, but this only works when the file is only included by one class. If multiple classes include the header then the linker complains that the symbol representing my function is multiply defined.
So I tried sticking a bare function in a header file and declaring the function as static. I think this works, but I don't know why, and I'm not about to just throw around the static keyword without understanding what its doing.
Is this the proper solution to my problem? If so, could someone explain what the static keyword is doing in this context? If not, what is the proper solution to this problem?
Thanks for the help.
Here is an example of what I think might be the right solution:
1 2 3 4 5 6 7 8 9 10
|
//string_util.h
#pragma once
#include <cstdlib>
#include <string>
using namespace std;
static string strDouble(string str)
{
return str.append(str);
}
|