Define members of a class using class's namespace in c++

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Define members of a class using class's namespace in c++



I have few classes with long names as well as their member functions. In C++ there is a trick which allows you to use one namespace and turn:


namespace_name::foo()



into


using namespace namespace_name;
foo()



For clarity of the code I'm wondering if there is a similar way to substitute definitions of functions:


LongClassName::LongFunctionName() {}



I'm sorry if I used improper vocabulary, but had no idea how to describe the problem.





How about LongClassName son; /* son = show object name */ son.LongFunctionName();? At least a part of the "long" name have been shortened.
– Some programmer dude
12 mins ago


LongClassName son; /* son = show object name */ son.LongFunctionName();





using ShortName = LongName;
– Antoine Morrier
11 mins ago


using ShortName = LongName;





This might be what you're looking for: stackoverflow.com/a/9864472/3292279
– Alex Johnson
4 mins ago




1 Answer
1



In C++ there is a trick which allows you to use one namespace and turn



This "trick" allows you to call function without specifying full function name with namespace. For function definition that does not work neither for namespace nor class. For namespace level function you either have to put that function definition inside namespace or mention it explicitly:


namespace foo {
void bar(); // foo::bar() declared
}

// you can define it as this
namespace foo {
void bar() {}
}

// or this
void foo::bar() {}

// this does not work
using namespace foo;
void bar() {} // ::bar() is defined here not foo::bar()



for class method definition - class name must be always used (and possibly namespace as well if any).






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived