Sign in to follow this  

Shortcut to :: all the time?

This topic is 3103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
I've never had any issue with this. It takes an extra 2 seconds and is more clear.

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
Sign in to follow this