Solved

Non-local class, namespace scope, function name must be unqualified.

Posted on 2013-01-24
3
499 Views
Last Modified: 2013-01-24
Hello Experts!

Found such a description and I do not really understand it...

However, you can define a function in a friend declaration. The class must be a non-local class. The function must have namespace scope, and the function name must be unqualified. The following example demonstrates this:

1. what is non-local class?
2. The function must have namespace scope- what does it mean?
3. Function name must be unqualified- what does it mean?


class A {
  void g();
};

void z() {
  class B {
    friend void f() { }; // error
  };
}

class C {
  friend void A::g() { } // error
  friend void h() { }
};

Open in new window



Thank you!
0
Comment
Question by:panJames
  • 3
3 Comments
 
LVL 22

Expert Comment

by:ambience
ID: 38813560
1. what is non-local class?

A class declared inside a function or method is non-local. It can not be used outside the defining scope.
0
 
LVL 22

Expert Comment

by:ambience
ID: 38813574
3. Function name must be unqualified?

These are qualified function names

  friend void ::h() {
  }
  friend void Test::h() {
  }

This is not

  friend void h() {
  }

WRT - friends that this means is

void f() {}

namespace Test {

      void f() {}

      class C
      {
                // definition must be unqualified
            friend void h() {
                  cout << "From h" << std::endl;
            }
            friend void Test::f(); // qualified declaration is allowed
            friend void ::f(); // qualified declaration is allowed

      public:
            void method()
            {
                  h();
            }
      };

}
0
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 38813594
2. The function must have namespace scope- what does it mean?

This should explain

namespace Test {

	void h();  // h is declared in namespace - not defined!

	class C 
	{
	public:
                // this is inside C but its definition of h in the namespace's scope
		friend void h() { 
			cout << "From h" << std::endl;
		}
	};

}


int main() 
{
	Test::h();
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question