Public Group

# Shortcut to :: all the time?

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

## 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 on other sites
There is no such a shortcut in C++, sorry.

##### Share on other sites
Oh, that's annoying. It would be really useful, I assume.
Thanks for the quick reply anyway.
-T.C.D.

##### Share on other sites
Quote:
 Original post by The Communist DuckOh, 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 on other sites
I've never had any issue with this. It takes an extra 2 seconds and is more clear.

##### Share on other sites
Quote:
 Original post by The Communist DuckOh, 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.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632984
• Total Posts
3009713
• ### Who's Online (See full list)

There are no registered users currently online

×