Solved

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

Posted on 2013-01-24
3
490 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now