Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way of pulling a class's members into your namespace with "using"?

I'm in a situation where I need to refer to one class in another, and I am constantly doing SomeClass:: this and whatever. Is there any way to do something similar to using namespace but for a class?

I've tried:

using MyClass; // Namespace qualified name is required.
using MyClass::; // Expected an identifier
using namespace MyClass; // Name must be a namespace name
using namespace MyClass::; // Name followed by :: must be a class or namespace name

I know one of the newer features in C++ is inheriting base class features, like using BaseClass::BaseClass, there seems to be a lot of ways to use using. I was wondering if I can do this class thing? Actually, it may just be a good idea to use a namespace instead of a class, if it's completely static.

like image 780
Zebrafish Avatar asked Nov 26 '25 23:11

Zebrafish


1 Answers

Short answer,

No You can't.

Long Answer:

Your expressions (with the errors):

using MyClass; // Namespace qualified name is required.
using MyClass::; // Expected an identifier
using namespace MyClass; // Name must be a namespace name
using namespace MyClass::; // Name followed by :: must be a class or namespace name

:would not work. Yes, of course, one of the jobs of the scope resolution operator is to make unqualified-name-lookup search in certain namespace-scopes. However, to make that same behavior possible for classes (outside the scope of a class or its derived classes), C++ would have to answer at least two questions:

  • Every name declared within a class that is accessed outside it will undergo access checking. So in trying to make unqualified name lookup access names in the so-called "class namespace", how and when should access checking be done?
  • How do we deal with non-static members?
like image 109
WhiZTiM Avatar answered Nov 29 '25 12:11

WhiZTiM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!