Splitting namespace header and definition.

Jun 28, 2010 at 8:49pm
Instead of putting a bunch of related static functions into a class I want to just group them under a namespace instead. Is it possible to split the function declarations and definitions across files as with a class(i.e., put all the declarations in a .h file and all the definitions in a .cpp file), and if so, is there any special syntax required? Is this even the appropriate approach to handling the grouping of related static-only functions?
Jun 28, 2010 at 9:02pm
Why do you want to do that? Don't forget that the name of your class also works as a namespace. You can easily tell the difference between a static and a member function call because the latter must be called from an object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MyClass
{
public:
    static void my_static_func();
    void my_mem_func();
};

//...

int main()
{
    MyClass object;
    object.my_mem_func();

    MyClass::my_static_func();

    return 0;
}

EDIT: Oh, sorry, I completely misunderstood what you said...

No, there's no special syntax required for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

//definitions, you might as well
//put them in a/some header(s) file

namespace asdf
{
    void f1();
    void f2();
}

namespace qwerty
{
    void f1();
}

//declarations, you might as well
//put them in several cpp files

void asdf::f1()
{
    cout << "asdf::f1" << endl;
}

void asdf::f2()
{
    cout << "asdf::f2" << endl;
}

void qwerty::f1()
{
    cout << "qwerty::f1" << endl;
}

//*************************

int main()
{
    using asdf::f2;

    asdf::f1();
    qwerty::f1();
    f2();

    cin.get();
    return 0;
}
Last edited on Jun 28, 2010 at 9:13pm
Jun 28, 2010 at 9:11pm
I vaguely remember reading a recommendation to use a namespace instead of a class to group functions if all the functions are static. I don't have a personal preference, so I could just as easily use a class, instead.
Jun 28, 2010 at 9:19pm
What exactly do you want to do? You want these functions to be part of a class or not? What I understood from your first post is that you have a bunch of related functions and you want to decide if you'll put them inside a class as static functions or inside a plain namespace.
Last edited on Jun 28, 2010 at 9:19pm
Jun 28, 2010 at 9:38pm
Here is what I do

1
2
3
4
5
6
7
8
9
10
11
12
13
// galik.h
#ifndef _GALIK_H
#define _GALIK_H

#include <string>

namespace galik {

void func(); // prototypes

} // namespace galik

#endif // _GALIK_H 


1
2
3
4
5
6
7
8
9
10
11
12
// galik.cpp
#include "galik.h"

namespace galik {

void func() // definition
{
	// do stuff
}

} // namespace galik
Jun 29, 2010 at 1:28pm
m4ster r0shi,
yes, that is what I was asking. My inclination would be to put them in a class, but I remember someone saying that it's unnecessary to create an new class whose only purpose would be to contain static functions. So that's why I opted to try a namespace instead, and that's why I was wondering if a namespace could/should be constructed similarly to a class, with a split declaration and definition.

Galik,
thanks for the example! I will try that.
Jun 29, 2010 at 3:57pm
If the functions are to be accessible to users of the class, then I'd leave them as static members of
the class. If the functions are not to be accessible to users of the class, then I'd move them entirely
to an unnamed namespace in the .cpp file and the header file make no mention of them. If they're
not usable by the user, then the user should have to see them.
Topic archived. No new replies allowed.