friend default constructor

Discussion in 'C++' started by mc, Jul 10, 2008.

  1. mc

    mc Guest

    Hello,

    I'm trying to create a class (A) with a default constructor friend to
    another one (B) as follows:

    class A
    {
    public:
    A(int a);
    ~A();

    private:
    A(); // default constructor
    }

    class B
    {
    // must be able to use A::A()
    }

    What I need is that only A and B can use the private default constructor
    (A::A()). By adding a "friend class B;" in class A, I got it to work but I
    do not want B to be able to access the whole of A. Is there a way to do
    this? Thank you in advance.

    Regards,

    MC
     
    mc, Jul 10, 2008
    #1
    1. Advertising

  2. mc

    mc Guest

    Thank you.

    "Victor Bazarov" <> wrote in message
    news:g55h5k$7sc$...
    > mc wrote:
    >> Hello,
    >>
    >> I'm trying to create a class (A) with a default constructor friend to
    >> another one (B) as follows:
    >>
    >> class A
    >> {
    >> public:
    >> A(int a);
    >> ~A();
    >>
    >> private:
    >> A(); // default constructor
    >> }
    >>
    >> class B
    >> {
    >> // must be able to use A::A()
    >> }
    >>
    >> What I need is that only A and B can use the private default constructor
    >> (A::A()). By adding a "friend class B;" in class A, I got it to work but
    >> I do not want B to be able to access the whole of A. Is there a way to
    >> do this? Thank you in advance.

    >
    > You need a proxy friend (or a friend proxy). There is no direct way, I
    > believe, you would have to rely on copy-construction (and RVO).
    >
    > class A
    > {
    > public:
    > class defaultAmaker
    > {
    > // everything is private
    > static A makeA() { return A(); }
    > friend class B;
    > };
    >
    > friend class defaultAmaker;
    > ...
    > };
    >
    > class B
    > {
    > A a = A::defaultAmaker::makeA(); // constructs A using A::A()
    > }
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
     
    mc, Jul 10, 2008
    #2
    1. Advertising

  3. mc

    Guest

    On Jul 10, 1:28 pm, "mc" <> wrote:
    > Hello,
    >
    > I'm trying to create a class (A) with a default constructor friend to
    > another one (B) as follows:
    >
    > class A
    > {
    > public:
    >     A(int a);
    >     ~A();
    >
    > private:
    >     A();   // default constructor
    >
    > }
    >
    > class B
    > {
    >   // must be able to use A::A()
    >
    > }
    >
    > What I need is that only A and B can use the private default constructor
    > (A::A()).  By adding a "friend class B;" in class A, I got it to work but I
    > do not want B to be able to access the whole of A.  Is there a way to do
    > this?  Thank you in advance.
    >
    > Regards,
    >
    > MC


    you could also try this:


    class B; // forward declare B but define it later

    class A
    {
    public:
    A(int a);
    ~A();

    private:
    A(); // default constructor

    friend class B; // say B is a friend

    }

    class B // now define B
    {
    // must be able to use A::A()

    }
     
    , Jul 11, 2008
    #3
  4. mc

    mc Guest

    Victor's correct: B can see everything in A and the forward declaration is
    not needed (at least with GCC)


    "Victor Bazarov" <> wrote in message
    news:g57hp5$ojg$...
    > wrote:
    >> On Jul 10, 1:28 pm, "mc" <> wrote:
    >>> Hello,
    >>>
    >>> I'm trying to create a class (A) with a default constructor friend to
    >>> another one (B) as follows:
    >>>
    >>> class A
    >>> {
    >>> public:
    >>> A(int a);
    >>> ~A();
    >>>
    >>> private:
    >>> A(); // default constructor
    >>>
    >>> }
    >>>
    >>> class B
    >>> {
    >>> // must be able to use A::A()
    >>>
    >>> }
    >>>
    >>> What I need is that only A and B can use the private default constructor
    >>> (A::A()). By adding a "friend class B;" in class A, I got it to work
    >>> but I
    >>> do not want B to be able to access the whole of A. Is there a way to do
    >>> this? Thank you in advance.
    >>>
    >>> Regards,
    >>>
    >>> MC

    >>
    >> you could also try this:
    >>
    >>
    >> class B; // forward declare B but define it later
    >>
    >> class A
    >> {
    >> public:
    >> A(int a);
    >> ~A();
    >>
    >> private:
    >> A(); // default constructor
    >>
    >> friend class B; // say B is a friend
    >>
    >> }
    >>
    >> class B // now define B
    >> {
    >> // must be able to use A::A()
    >>
    >> }

    >
    > This solution allows 'B' to see _everything_ in 'A', not just its private
    > default c-tor.
    >
    > BTW, try removing the forward declaration of 'B'.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
     
    mc, Jul 11, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    486
    Mike Wahler
    Jan 25, 2004
  2. ankit_jain_gzb
    Replies:
    4
    Views:
    1,226
    ankit_jain_gzb
    May 29, 2005
  3. Replies:
    9
    Views:
    995
    Alf P. Steinbach
    Mar 6, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,342
  5. Peter
    Replies:
    2
    Views:
    310
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page