[comp.lang.c++] Objects with only private constructors as static members

craigf@icsi.ICSI.Berkeley.EDU (Craig Federighi) (02/21/91)

    The question regards static class members and private classes
(i.e. a class with only a private constructor).  Admittedly obscure,
but...

I first created a private class PRIV that had another class PUBL
as its friend:

class PRIV
{
    friend class PUBL;

private:
    PRIV( int x ) : value( x ) {};
    int value;
}

I next created the friend class PUBL and defined a static member of
type PRIV:

class PUBL:
{
private:
    static PRIV p;
    ...
}

In the module where I implemented class PUBL, I also attempted to
define the static member p:

PRIV PUBL::p( 5 );

The compiler issued an error in regards to this line:

g++ issues the following error messages:
    t.c:12: constructor `PRIV::PRIV (int)' is private
    t.c:12: in base initialization for class `PRIV'
CFRONT and Borland Turbo C++ also give similar errors.

This is especially bizzare because I CAN create non-static instances
of PRIV within PUBL.  i.e.:
    class PUBL {
    public:
	    PUBL( int z )
		    : p(z)
	    {
	    }
    private:
	    PRIV p;
    };

And I can, of course, declare an instance of a static member that has
a public constructor. i.e.
    class PUBL {
    public:
	    PUBL( ) {}
    private:
	    String p;
    };
    
    // and in the .cc file...
    static String PUBL::p();
    
What is going on here?  Did CFront screw up the implementation of this and
then everyone else just followed suit, or does the formal language 
specification actually disallow the instanciation of static members with 
private (but friend) constructors?  Why?!?

Since I don't get to read news very often, could you please just send me
any replies through the mail?

Thanks,

Craig Federighi
craig@icsi.berkeley.edu