Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3661128
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:16:22+00:00 2026-05-19T01:16:22+00:00

Suppose I have a class F that should be friend to the classes G

  • 0

Suppose I have a class F that should be friend to the classes G (in the global namespace) and C (in namespace A).

  • to be friend to A::C, F must be forward declared.
  • to be friend to G, no forward declaration of F is necessary.
  • likewise, a class A::BF can be friend to A::C without forward declaration

The following code illustrates this and compiles with GCC 4.5, VC++ 10 and at least with one other compiler.

class G {
    friend class F;
    int g;
};

// without this forward declaration, F can't be friend to A::C
class F;

namespace A {

class C {
    friend class ::F;
    friend class BF;
    int c;
};

class BF {
public:
    BF() { c.c = 2; }
private:
    C c;
};

} // namespace A

class F {
public:
    F() { g.g = 3; c.c = 2; }
private:
    G g;
    A::C c;
};

int main()
{
    F f;
}

To me this seems inconsistent. Is there a reason for this or is it just a design decision of the standard?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:16:22+00:00Added an answer on May 19, 2026 at 1:16 am

    C++ Standard ISO/IEC 14882:2003(E)

    7.3.1.2 Namespace member definitions

    Paragraph 3

    Every name first declared in a
    namespace is a member of that
    namespace
    . If a friend declaration in
    a non-local class first declares a
    class or function
    (this implies that the name of the class or function is unqualified) the friend class
    or function is a member of the
    innermost enclosing namespace.

    // Assume f and g have not yet been defined.
    void h(int);
    template <class T> void f2(T);
    namespace A {
       class X {
       friend void f(X);  //  A::f(X) is a friend
          class Y {
             friend void g();  //  A::g is a friend
             friend void h(int);  //  A::h is a friend
             //  ::h not considered
             friend void f2<>(int);  //  ::f2<>(int) is a friend
          };
       };
       //  A::f, A::g and A::h are not visible here
       X x;
       void g() { f(x); }  // definition of A::g
       void f(X) { /* ... */}  // definition of A::f
       void h(int) { /* ... */ }  // definition of A::h
       //  A::f, A::g and A::h are visible here and known to be friends
    }
    

    Your friend class BF; is a declaration of A::BF in namespace A rather than global namespace. You need the global prior declaration to avoid this new declaration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose you have to model several classes that should have composite properties like dimensions
Suppose I have a class that processes some data: class SomeClass { public: void
Suppose I have a Python class that I want to add an extra property
Suppose I have a class Customer that is mapped to the database and everything
Suppose I have a Base class and a Child class that inherits from Base
Possible Duplicate: How to Deserialize XML document Suppose that I have a class that
Suppose I have a Generic abstract class that provides some default constructor functionality so
Suppose I have a static method of my class that returns an object of
Suppose that we have a class called class1. The class1 has a method called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.