tags:

views:

112

answers:

3

General question: Where is usually defined class, namespace.

My case:

I am only using this maps in my class implementation:

std::map<const std::pair<string, string>,const string*>

and I wonder where is it good place to put it in the class (in public:) or in the enclosing header file? I don't have namespaces

+9  A: 

If it is in your class only, I usually put it at the top of the private section:

class Foo
{
public:
    void some_functions(void);

private:
    typedef std::pair<std::string, std::string> StringPair;
    typedef std::map<StringPair, std::string> StringPairMap;

    StringPairMap _stringMap;
}

To clarify, as with most things you want to specify these things as local as possible. Variables should do this: you don't define int i for your for-loop until you've reached the loop, etc..

Likewise, if your class uses these typedef's internally, do what I said above. If only a specific function in your class needs that typedef, place the typedef at the beginning of that function.

If you need to expose this typedef to clients of the class, I like to place the typedef's at the top of the public section.

GMan
I'd upvote, but that `void` in your parameter list makes my eyes sore.
avakar
I've always found it clearer. This is a function call: `int i = perform_calc()`. There is no `void` there, so by putting a `void` there I no longer have to remember context when I read functions ("is this a function call or function definition?")
GMan
When you gaze into the void the void gazed back into you.
Eugene
Also, I don't like the look of some functions just being empty `()` and some with parameters `(int i, float f)`. I like things being consistent, so I like always having a parameter list, so `void` works nicely.
GMan
I don't think I understand your point. I think its obvious that `int i = perform_calc();` is a function call just as it's obvious that `int perform_calc();` is a declaration. Regarding your second comment, why do you consider the sequence `(void), (int), (int, int), ...` to be more consistent than `(), (int), (int, int), ...`?
avakar
Because there is nothing inside the parenthesis, on the second version. By consistent, I meant there is always something in the parenthesis, and being explicit about the function taking nothing makes it easier to read. I think it's more of me liking the consistency of something in the parenthesis, and therefore being easier for me to read, rather than deducing what it's purpose is.
GMan
+1  A: 

Put it where you use it. If you use it in the private section of the header file, declare the typedef there. If you use it only in the implementation code in the .cpp file, declare the typedef there.

Fred Larson
A: 

It depends, do you need this type as in the public interface to the class? If so then I would declare it inside the public section of the class. If you only use it in the private part of the class then declare it there. And if you only use it in the implementation then declare it there. In general never declare it in global namespace in a header file (or if you do make sure to prefix the name with something that won't clash).

The rule of thumb here is to make the deceleration visible/accessible to the smallest subset of your program as possible.

Matt Price