how to declare the template parameter class as friend class - c++

This is a discussion on how to declare the template parameter class as friend class - c++ ; template <class C> class Session { public: protected: friend class C; //the line has no effect , compiled with vc8 sp1, it will report no public construct error. // It will ok if I changed to friend class Connection ; ...

+ Reply to Thread
Results 1 to 6 of 6

how to declare the template parameter class as friend class

  1. Default how to declare the template parameter class as friend class

    template <class C>
    class Session
    {
    public:

    protected:
    friend class C; //the line has no effect , compiled with vc8 sp1, it
    will report no public construct error.
    // It will ok if I changed to friend class
    Connection ;
    Session(C & c):m_c(c)
    {
    }

    C &m_c;
    };


    class Connection
    {
    public:
    Connection()
    {
    m_session = Session<Connection>(*this);
    }

    private:
    Session<Connection> m_session;
    }

    //so , is there any other way to resolve it ?


  2. Default Re: how to declare the template parameter class as friend class

    fatihwk wrote:
    > template <class C>
    > class Session
    > {
    > public:
    >
    > protected:
    > friend class C; //the line has no effect , compiled with vc8 sp1, it
    > will report no public construct error.
    > // It will ok if I changed to friend class
    > Connection ;
    > Session(C & c):m_c(c)
    > {
    > }
    >
    > C &m_c;
    > };
    >
    >
    > class Connection
    > {
    > public:
    > Connection()
    > {
    > m_session = Session<Connection>(*this);
    > }
    >
    > private:
    > Session<Connection> m_session;
    > }
    >
    > //so , is there any other way to resolve it ?


    You cannot make any type which depends on a template
    parameter a friend of the relevant class.

    You could make a private base "interface" class for
    Session, and then from the Session code pass a reference
    to SessionBase to the Connection object for its use.

    -- James

  3. Default Re: how to declare the template parameter class as friend class

    "fatihwk" <fatihwk@gmail.com> wrote in message
    news:1190012404.212235.89050@r29g2000hsg.googlegroups.com...
    > template <class C>
    > class Session
    > {
    > public:
    >
    > protected:
    > friend class C; //the line has no effect , compiled with vc8 sp1, it
    > will report no public construct error.
    > // It will ok if I changed to friend class
    > Connection ;
    > Session(C & c):m_c(c)
    > {
    > }
    >
    > C &m_c;
    > };
    >
    >
    > class Connection
    > {
    > public:
    > Connection()
    > {
    > m_session = Session<Connection>(*this);
    > }
    >
    > private:
    > Session<Connection> m_session;
    > }
    >
    > //so , is there any other way to resolve it ?


    Even if you remove the protected in class Session, it fails to compile. Try
    this instead, even though I get warning messages

    Connection(): m_session(*this)
    {
    }



  4. Default Re: how to declare the template parameter class as friend class

    "James Dennett" <jdennett@acm.org> wrote in message
    news:WmqHi.161581$zz2.148101@newsfe12.phx...
    > fatihwk wrote:
    >> template <class C>
    >> class Session
    >> {
    >> public:
    >>
    >> protected:
    >> friend class C; //the line has no effect , compiled with vc8 sp1, it
    >> will report no public construct error.
    >> // It will ok if I changed to friend class
    >> Connection ;
    >> Session(C & c):m_c(c)
    >> {
    >> }
    >>
    >> C &m_c;
    >> };
    >>
    >>
    >> class Connection
    >> {
    >> public:
    >> Connection()
    >> {
    >> m_session = Session<Connection>(*this);
    >> }
    >>
    >> private:
    >> Session<Connection> m_session;
    >> }
    >>
    >> //so , is there any other way to resolve it ?

    >
    > You cannot make any type which depends on a template
    > parameter a friend of the relevant class.
    >
    > You could make a private base "interface" class for
    > Session, and then from the Session code pass a reference
    > to SessionBase to the Connection object for its use.


    James, this compiles for me. I'm using MSVC++ .net 2003. Should it
    compile?

    template <class C>
    class Session
    {
    public:

    protected:
    friend C;
    Session(C & c):m_c(c)
    {
    }

    C& m_c;
    };


    class Connection
    {
    public:
    Connection(): m_session(*this)
    {
    }

    private:
    Session<Connection> m_session;
    };

    int main()
    {
    Connection Foo;
    }



  5. Default Re: how to declare the template parameter class as friend class

    Jim Langston wrote:
    > "James Dennett" <jdennett@acm.org> wrote in message
    > news:WmqHi.161581$zz2.148101@newsfe12.phx...
    >> fatihwk wrote:
    >>> template <class C>
    >>> class Session
    >>> {
    >>> public:
    >>>
    >>> protected:
    >>> friend class C; //the line has no effect , compiled with vc8 sp1, it
    >>> will report no public construct error.
    >>> // It will ok if I changed to friend class
    >>> Connection ;
    >>> Session(C & c):m_c(c)
    >>> {
    >>> }
    >>>
    >>> C &m_c;
    >>> };
    >>>
    >>>
    >>> class Connection
    >>> {
    >>> public:
    >>> Connection()
    >>> {
    >>> m_session = Session<Connection>(*this);
    >>> }
    >>>
    >>> private:
    >>> Session<Connection> m_session;
    >>> }
    >>>
    >>> //so , is there any other way to resolve it ?

    >> You cannot make any type which depends on a template
    >> parameter a friend of the relevant class.
    >>
    >> You could make a private base "interface" class for
    >> Session, and then from the Session code pass a reference
    >> to SessionBase to the Connection object for its use.

    >
    > James, this compiles for me. I'm using MSVC++ .net 2003. Should it
    > compile?
    >
    > template <class C>
    > class Session
    > {
    > public:
    >
    > protected:
    > friend C;
    > Session(C & c):m_c(c)
    > {
    > }
    >
    > C& m_c;
    > };
    >
    >
    > class Connection
    > {
    > public:
    > Connection(): m_session(*this)
    > {
    > }
    >
    > private:
    > Session<Connection> m_session;
    > };
    >
    > int main()
    > {
    > Connection Foo;
    > }


    No, it should not. (In C++03, you need "friend class Foo;"
    to make a class a friend, and you can't make the class
    named by a template a friend.)

    g++ on my local machine diagnoses this with "error: using
    template type parameter 'C' after 'class'" and confusingly
    "error: friend declaration does not name a class or function".

    -- James

  6. Default Re: how to declare the template parameter class as friend class

    this is a sample code, I just want to show the "friend" problem, so
    there are something wrong.
    Now I try to compile on different compilers,all are failed.
    on g++ 3.4.4 cygwin, just one line error: test.cpp:7: error:
    using typedef-name `C' after `class'

    on g++ 3.3.3 suse linux :
    testtemp.cpp:7: error: using template type parameter `C' after `class'
    testtemp.cpp:7: error: ISO C++ forbids declaration of `type name' with
    no type
    testtemp.cpp: In constructor `Connection::Connection()':
    testtemp.cpp:9: error: `Session<C>::Session(C&) [with C = Connection]'
    is
    protected
    testtemp.cpp:21: error: within this context

    so it means the class after friend must be a "real" class. not
    typedef-name. so in the language what's the different between
    original class and the typedefined class

    this code compile ok on gcc 3.3.3 suse linux, but failed on cygwin
    3.4.4.

    class Connection;
    template <class C >
    class Session
    {
    public:
    typedef Connection Conn;
    protected:
    friend class Conn; //here , we use the typedef-name
    Session(C & c):m_c(c)
    {
    }

    C &m_c;
    };


+ Reply to Thread

Similar Threads

  1. Does friend class declaration also declare the class?
    By Application Development in forum c++
    Replies: 1
    Last Post: 10-23-2007, 04:11 PM
  2. Howto declare a friend function to a nested class
    By Application Development in forum c++
    Replies: 4
    Last Post: 09-30-2007, 10:33 AM
  3. Replies: 4
    Last Post: 08-20-2007, 05:27 AM
  4. Replies: 10
    Last Post: 07-31-2007, 08:49 PM
  5. Replies: 1
    Last Post: 06-14-2007, 10:51 AM