What is the meaning of multiple class methods separated by "::"? [duplicate] - c++

This question already has answers here:
What does the "::" mean in C++?
(4 answers)
Closed last year.
I found this line of code where I'm working, but I don't quite understand its meaning:
virtual method1::method2::method3 f() = 0;
In the main function, I have method1::method2::method3.g1().g2(). I really don't understand. I know method::A where A is a class.

:: is a scope operator.
You may need to append multiple of them either when using nested classes (a class defined within another class) or when you use namespaces. Resolution is done the same way.

:: is the scope resolution operator. It allows you to statically traverse scopes such as namespaces and classes in order to reference the identifier you want.
I think what you have is not methods but namespaces and classes.

Related

What is the use of a static function (not class member) in c++? [duplicate]

This question already has answers here:
What is a "static" function in C?
(11 answers)
Why are unnamed namespaces used and what are their benefits?
(6 answers)
Closed 3 years ago.
So, I was reading some book and it had source code with a single cpp file, no classes and a bunch of static functions.
After some searches, I mostly see material about static member functions, which I know what they do and doesn't provide me any answer.
I also found something about anonymous namespaces vs static functions, but didn't quite understand the point.
So, can anybody out there provide me some insights on what are static non member functions, what's their uses or why to use them?
One use of static free function is to prevent redeclaration of the function in different translation units which causes link errors.
What is the use of a static function
Static functions or more generally: functions with internal linkage are useful in encapsulating the function by preventing calls to it from other translation units than the one where it is defined.
It also helps prevent name collisions between functions defined and used in different translation units.
I also found something about anonymous namespaces vs static functions
Anonymous namespace are another, newer way of declaring functions with internal linkage. Unlike keyword static, they also allow definition of types with internal linkage.

C++ "::" without class name [duplicate]

This question already has answers here:
scope resolution operator without a scope
(6 answers)
Closed 5 years ago.
I came across the following code structure in C++:
uint32_t AClass::Action(....)
{
..
status = ::Action(...);
..
}
I am not sure what ::Action() means. Which class does it belongs to? NOTE: the argument list of ::Action(...) is different from AClass::Action(...).
The leading :: just means that Action here refers to a non-member function in the global namespace, instead of referring to AClass::Action in the current namespace.
::Action() mean it is a function under the the global namespace.

what does "::functionName()" imply in c++? [duplicate]

This question already has answers here:
What is the meaning of prepended double colon "::"?
(9 answers)
:: without a namespace
(3 answers)
Why is :: (scope) used with empty left-hand operand? [duplicate]
(6 answers)
Closed 9 years ago.
I came across a piece of code which looks like this:
::GetSystemDirectory(buffer, MAX_PATH);
I've never seen a function call preceded by an empty ::. I've always seen them being used with namespaces.
Can someone please explain me what does an empty :: mean ?
It's the scope resolution operator. With nothing in front of it, it indicates global scope.
So for instance, suppose you have a class that defines its own GetSystemDirectory method. Within the code of a method of that class, to call the global one, you'd need the :: in front of it, otherwise by default you'd get the one specific to the class. (And similarly for namespaces.)

What does "::" mean in c++? [duplicate]

This question already has answers here:
Why does C++ need the scope resolution operator?
(7 answers)
Closed 9 years ago.
I have code I am going over that looks like this:
foo::foofa(string n){
loadFoo(fn);
}
What does the foo::foofa mean? I do not quite understand what does :: do? Thanks.
EDIT: Also, is there another way to write this without the :: or is it required?
:: is the scope operator to used to identify and specify the context that an identifier refers to.
using a very simple google search, IBM describes it as:
The :: (scope resolution) operator is used to qualify hidden names so
that you can still use them. You can use the unary scope operator if a
namespace scope or global scope name is hidden by an explicit
declaration of the same name in a block or class.
I do not quite understand what does :: do?
It's the scope resolution operator.
If foo is a class (or a namespace), and foofa is something declared inside that class, then within the class you can refer to it simply as foofa. But outside the class, you need to use this operator to specify that you mean this particular foo::foofa; there could be others scoped inside other classes or namespaces.
Also, is there another way to write this without the :: or is it required?
It's required outside the class definition. You could define the function inside the class:
class foo {
void foofa(string n) { // No foo:: needed here
loadFoo(n);
}
};
If foo is a namespace, then you can also use using to avoid the need for :: but that's often a bad idea, so I won't show you how.
::
is the scope resolution operator.
Quoted from Scope Resolution Operator
The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.
You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator.
:: indicates a scope. So either a namespace or class name. For instance if you want to access the sort function in the standard (std) namespace you would use
std::sort

Difference between function and method in C++? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Functions or methods?
I was thinking that they were both the same but I'm reading a book on C++ and I'm not really sure how they are different. Sorry I know this question has been asked but I'm still not really sure if they are different or not. Can someone please explain? Thanks.
Assuming you mean 'member functions' by 'methods', have a look at this
Member functions are functions declared inside a class.
THe difference between ordinary functions and (non-static) member functions is that non-static member functions take an implicit parameter: the pointer to the object they are being called on (this)
The C++ language definition talks about "functions" and "member functions". It does not talk about "methods". So the meaning of "function" and of "member function" is well defined. The meaning of "method" for C++ is whatever you think it means, and it's often used in precisely that way, that is, as a fuzzy term for "something I think I can call", without a precise meaning.