views:

86

answers:

5

If in C++ I have a class longUnderstandableName. For that class I have a header file containing its method declaration. In the source file for the class, I have to write longUnderstandableName::MethodA, longUnderstandableName::MethodB and so on, everywhere.

Can I somehow make use of namespaces or something else so I can just write MethodA and MethodB, in the class source file, and only there?

+1  A: 

Nope. Namespaces and class names are two different things. You cannot do what you want. Technically, you could define a method within a class declaration, java style, but that would put all your code in the header file, meaning that any change of the code could trigger a large recompile due to code dependencies.

Additional note: in general, classes start with capitalized letters, and methods start with lowercase letter.

Stefano Borini
Actually, if one wants to follow the conventions used in the standard library, one does not use capitalized letters at all. (So long as one is consistent one is not incorrect)
Billy ONeal
+1  A: 

I'm not sure I'd recommend it, but you could use a macro like:

#define sn LongUnderstandableName

void sn::MethodA(parameters) { ... }
int sn::MethodB(parameters) { ... }

and so on. One of the bad points of macros is that they don't respect scope, but in this case, the scope you (apparently) want is the source file, which happens to correspond (pretty closely) with the scope of a macro.

Jerry Coffin
+1, it's a fast solution, but I would not recommend it either. It's a recipe for disaster.
Stefano Borini
Yes -- I'd be much more likely to define it as a macro in the editor, so I only have to type the short name, and it's automatically expanded to the real one -- but nobody else has to be aware of that.
Jerry Coffin
You could use a typedef instead.
Ken Bloom
A: 

Well, yes, once you understand namespaces.

Instead of naming your class MyBonnieLiesOverTheOcean, instead set up the following:

namespace My { namespace Bonnie { namespace LiesOverThe {
   class Ocean { ... };
} } }

Now, when defining your methods, you put the same namespaces around the whole file, and you write:

Ocean::SomeMethod() ...

When using the class from outside all the namespaces, it's:

My::Bonnie::LiesOverThe::Ocean

If you need to reference a lot of things from some other namespace in some source file, you can use the 'use' directive to ditch the prefixes.

bmargulies
That's a nice idea but it must suck to type all those :: :)
Stefano Borini
Well, that's why we have 'use'.
bmargulies
Ah ok, yes. You can say `use My::Bonnie::LiesOverThe`; and then work on the last nesting level
Stefano Borini
+1  A: 

Inside the methods of the classes, you can use the name without qualification, anyway: just drop the longUnderstandableName:: prefix.

In functions inside the class source file that are not methods, I suggest to introduce file-scope static inline functions, like so:

inline type MethodA(type param){
    return longUnderstandableName::MethodA(param);
}

Then you can call MethodA unqualified; due to the inline nature, this likely won't cost any runtime overhead.

Martin v. Löwis
Yes, but this forces you to define the method inside the header, which is inefficient because it forces recompilation of all clients whenever the class changes.
Billy ONeal
No, it doesn't force you to do that. In fact, you shouldn't define the method in the header, but at the top of the class source file - else the shortcut would be globally available, which the OP doesn't want to happen.
Martin v. Löwis
+1  A: 
typedef longUnderstandableName sn;

Then you can call the methods as

sn::MethodA();
sn::MethodB();

This only works if longUnderstandableName is the name of a class. It works even if the class is deeply embedded in some other namespace.

If longUnderstandableName is the name of a namespace, then in the namespace (or source file) where you want to use the methods, you can write

using namespace longUnderstandableName;

and then call methods like

MethodA();
MethodB();

You should be careful not to use a using namespace foo; in header files, because then it pollutes every .cpp file that we #include the header file into, however using a using namespace foo; at the top of a .cpp file is definitely allowed and encouraged.

Ken Bloom