I have a singly linked list implementation as shown below:
Header
class SinglyLinkedList
{
struct Node
{
Node * _pNext;
int _data;
};
public:
Node * SomeFun(Node * ip1, Node * ip2);
// Some more methods here
};
Now when implementing one of the methods of this class
CPP
Node * SinglyLinkedList::SomeFun(Node * ip1, Node * ip2)
{
//Some code and return
}
The strange behavior I am not understanding is that when compiling, the compiler refuses to recognize the type "Node" in the return type unless I specify it as SinglyLinkedList::Node. But the same type for function arguments is recognized without specifying it explicitly.. Ideally I felt that in both cases there should be no need for this explicit specifying because Node is defined in the same class. Can anyone throw some light on this?
The scope resolution operator :: is used to identify and disambiguate identifiers used in different scopes. For more information about scope, see Scope.
Scope resolution operator in C++ can be used for: Accessing a global variable when there is a local variable with same name. Defining a function outside a class. Accessing a class's static variables.
The scope of struct type definitions ( with or without the use of typedef ) follows the same rules as variable declarations. Obviously this means that the struct definition must be within scope before variables of that type can be declared.
Which of the following is the scope resolution operator? a) . Explanation: :: operator is called scope resolution operator used for accessing a global variable from a function which is having the same name as the variable declared in the function. 2.
SinglyLinkedList::Node * SinglyLinkedList::SomeFun
Here, you are not in class-scope. But in parameters clause, or in function you are IN class-scope, so you shouln't qualify, that Node
is from class SinglyLinkedList
, since compiler already knows that.
n3376 3.3.7/1
The following rules describe the scope of names declared in classes.
The potential scope of a declaration that extends to or past the end of a class definition also ex- tends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions, nested class definitions, member func- tion definitions (including the member function body and any portion of the declarator part of such definitions which follows the declarator-id, including a parameter-declaration-clause and any default arguments
Adding to ForEveR's answer, this issue is solved in C++11 using trailing return clauses.
// Regular return style
SinglyLinkedList::Node* SinglyLinkedList::SomeFun(Node * ip1, Node * ip2) { ... }
// Trailing return style
auto SinglyLinkedList::SomeFun(Node * ip1, Node * ip2) -> Node * { ... }
In the regular style, you have not yet entered class scope (it only begins after the qualified name of the method has ended).
In the trailing return style however, the return type is in the class scope (like the arguments) and therefore the qualification not necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With