Shortcut to :: all the time?

Started by
4 comments, last by MaulingMonkey 14 years, 9 months ago
Hello. I am learning C++, so I assume this is almost certainly a newb question. You put class function prototypes and members in a .h file, and functions in a .cpp file with #include "class.h". Then, for the methods, you put Class::Method(int x){...}. Is there a way to not have to write Class:: all the time? Could you use using namespace Class?I know you can avoid std::cout by typing using namespace std. Thanks. -T.C.D
Advertisement
There is no such a shortcut in C++, sorry.
Oh, that's annoying. It would be really useful, I assume.
Thanks for the quick reply anyway.
-T.C.D.
Quote:Original post by The Communist Duck
Oh, that's annoying. It would be really useful, I assume.
For what its worth, the Visual Studio add-in Visual Assist X seems to have functionality you'd probably enjoy. I haven't used it myself, but many developers here swear by it. And from their tour (in particular the page "Enhanced Refactoring for C++") they have a feature that's probably as useful as the one you're looking for. That said it isn't free (there is a trial version you can use) and it won't work with the express editions of Visual Studio.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

I've never had any issue with this. It takes an extra 2 seconds and is more clear.
Quote:Original post by The Communist Duck
Oh, that's annoying. It would be really useful, I assume.

Other languages manage to avoid the need for seperate header files, allowing you to just put the methods inside the class directly with no drawbacks. For example, in C#, we'd do...

namespace Foo {    class Class {        public void Method( int x ) { ... }    }}


This gets rid of the "Class::" prefixes. We can actually do similar in C++ sometimes (just putting the class methods inside the class inside the header) -- but it brings in major headaches: your compile times bloat, and C++'s very limited circular dependency handling means you quickly run into errors which can only be fixed by separating the methods from the class in the standard way. Most of the time, it's easier to just separate them in the first place.

This topic is closed to new replies.

Advertisement