Scope Resolution Operator Without a Scope

scope resolution operator without a scope

It means global scope. You might need to use this operator when you have conflicting functions or variables in the same scope and you need to use a global one. You might have something like:

void bar();    // this is a global function

class foo {
void some_func() { ::bar(); } // this function is calling the global bar() and not the class version
void bar(); // this is a class member
};

If you need to call the global bar() function from within a class member function, you should use ::bar() to get to the global version of the function.

declaring scope resolution operator without including standart header

The scope resolution operator is as the name says, an operator, and is not part of any library but rather the basic syntax of the C++ language. It's hard to tell exactly what your issue is based off your question, but when you don't know what you're doing wrong it can be hard to find the right words to ask. Try reading Tutorials Point's article on namespaces, I find their tutorials very helpful and approachable for beginners https://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm

Implement class without scope resolution operator?

You can use pre-processor macros to make it easier.

#include "A.h"
#ifdef _
#undef _
#endif
#define _(fun) A::fun

int _(function)(int number)
{
return 2* number;
}

int _(foo)(void)
{
return 2;
}

However ...

You shouldn't necessarily do something just because you can.

Using a preprocessor macro to hide an important construct of the language is ill-advised. Here are some things to think about before you go down that path.

  1. The time spent writing <ClassName>:: fades in comparison to the amount time spent in writing rest of the code in any program that does something useful.

  2. The amount of time you will spend in debugging and maintaining a program will be much much higher than the amount of time you'll spend in writing <ClassName>::.

  3. When you have multiple classes in a .cpp file, you will have a hard time clearly seeing which function corresponds to which class if you adopt the same policy for all classes.

  4. When you are debugging, you will be frustrated that you won't be able to see the fully qualified function name when you step into a function.

  5. The loss of clarity is too expensive a price to pay in exchange for saving a few key strokes while implementing the member functions of a class.

I am speaking from experience. I work in a project where one of the class names is INR_DynamicallyTypedLowFidelityLeafComponentRepresentation. I wouldn't use a preprocessor macro to hide even that long a name.

Why do you use a Scope Resolution Operator when defining a class' method?

When you define a class:

struct foo {
void bar() {}
};

Then the full name of bar is ::foo::bar. The leading :: to refer to the global namespace can often be omitted. There is no bar in the global namespace, hence bar alone (or ::bar) does not name an entity and when you define the method out of line you need to tell what bar you mean:

 struct foo { 
void bar();
};
struct baz {
void bar();
};

void bar() {} // this defines a completely unrelated free function called bar

void foo::bar() {} // defines foo::bar
void baz::bar() {} // defines baz::bar

You need the scope resolution operator to state which method you want to define.

For more details I refer you to https://en.cppreference.com/w/cpp/language/lookup

Does C++ provide a way to access a class within a class without the scope resolution operator?

Yes, C++ offers a way to access the type. What you need is a type alias. Using

using bar_class_in_class = foo_class::bar_class_in_class;

allows you to use bar_class_in_class in place of foo_class::bar_class_in_class. This is scoped so if you do it in main, you can only see the alias in main


Do note that you can also "change the name" of the inner type

using my_type = foo_class::bar_class_in_class;

does the same thing as above, but you would use my_type as the type name. This can be a nice convenience.



Related Topics



Leave a reply



Submit