Solved

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

Posted on 2013-01-24
3
500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

691 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