Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why scope resolution necessary for struct define inside a class when returning as value?

Tags:

c++

scope

class

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?

like image 340
Arun Avatar asked Jun 06 '13 05:06

Arun


People also ask

What is the purpose of scope resolution operator?

The scope resolution operator :: is used to identify and disambiguate identifiers used in different scopes. For more information about scope, see Scope.

Why do we use scope resolution operator in C++?

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.

What is the scope of a struct?

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 in C++?

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.


2 Answers

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

like image 130
ForEveR Avatar answered Sep 20 '22 08:09

ForEveR


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.

like image 35
Matthieu M. Avatar answered Sep 18 '22 08:09

Matthieu M.