creating a library like cstdlib

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);
}




No, static is not the right solution.

When you write your own stuff, you need to create both a header and a source file. Compile the source file and link it into your project.

Here is an example.

mylib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// This file is the interface to your library.

// Use the #define guards instead of that #pragma once for maximim portability
#ifndef MYLIB_H
#define MYLIB_H

#include <string>
// Never "using namespace anything" in header files. It is not appropriate.
// (Remember, it is not up to you to dictate what goes in your user's
//  global namespace -- it is your user's choice what goes there.)

namespace mylib
  {
  // Also, you should always wrap your stuff in your own namespace.
  // This again gives the user the choice of what to put in his global namespace.
  // Also, it prevents linker conflicts with other libraries.

  // Notice how we fully-qualify all names used by your library.
  // Also notice how I modified the function somewhat... This is better.
  std::string strDouble( const std::string& s );

  }

#endif 

mylib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// This file is the code implementing your library.

// First, lets see the interface to your library.
// You can #include other stuff here too... This example is just simple enough that this is all that is needed
#include "mylib.h"

// It is fine to "using namespace anything" here in this file, since this file's global namespace is
// under your control -- whatever you "using" here cannot affect other parts of the program.
using namespace std;

namespace mylib
  {
  // Remember, keep your stuff (even private stuff) in your own namespace.
  // Symbols (variables, functions, etc) that you define here can conflict with other modules
  // at the linking stage unless you do this.

  string strDouble( const string& s )
    {
    return string( s ).append( s );
    }

  }

a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Here is an example of how you might use the library.

// The usual stuff
#include <iostream>
using namespace std;

// Now you are the user of the library -- so you can play with the program's global
// namespace all you want. If that means dumping everything in the library's namespace into
// the global namespace, as we do here, that's fine. You, as the user of the library, have
// that perrogative.
#include "mylib.h"
using namespace mylib;

int main()
  {
  string name;
  cout << "What is your name? " << flush;
  getline( cin, name );

  cout << "Hello there " << strDouble( name ) << "! :-)\n";
  cout << "Just Kidding, " << name << ". I'll treat you right.\n";
  return 0;
  }


If I had not said using namespace mylib; on line 12 then I would have had to qualify the statement on line 20:

20 cout << "Hello there " << mylib::strDouble( name ) << "! :-)\n";

Notice that this is exactly how the standard library works.

Hope this helps.
Wow, thanks - I've never learned so much about programming by reading so little..

Just out of curiosity, can you recommend any great books about c++ programming for someone like me who:
- recently graduated from university with a cisc degree
- knows the basics of c++ but doesn't know the pragmatic, production quality, state of the art methods for writing code and dealing with common problems

like something that I could read and just learn a ton about writing really good code?

Again, thanks.
closed account (NqG36Up4)
by "cisc" you mean computer and information sience ?! I'm at my second semster studing CS and these stuff are basics. you should know easily (at least the concept) of how your code compiles, get linked and even assemplied.

I would recommened the tutroial at this site and at cppreferrence as well and ofcourse write code as much as you can :)
LOL - I figured you were a computer science professor or someone with lots of real world programming experience.

What university are you at?

I went to University of Delaware - a good school but nothing close to MIT level. I think very very few students, if any, in their first year at Delaware would be able to give the explanation you just gave.
closed account (NqG36Up4)
turning sarcastic, aren't we? =]

First of all, reading my last post once again, I feel the need to apologize. I hadn't meant it in the way it sounds, it sounds arrogant and full of slef esteem. I just meant to say that IMO, if you wish really to understand how your code works you should understand all "levels" of the program and that's what I would expect from a grad, it is nothing personal.

btw, I'm studying at the Technion,Israel.
Topic archived. No new replies allowed.