Sign in to follow this  
The Communist Duck

Shortcut to :: all the time?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this