static member data + private c'tor

static member data + private c'tor

Post by David B. Hel » Fri, 01 Aug 2003 09:10:30



Consider:

struct X
{
    static X x;
private:
    X() { }

Quote:};

X::X x;

int main() { }

Is this legal code?  Why or why not?  gcc-3.3 says that X::X() is
private, but it seems to me that it should be accessible, since x
is declared (and defined?) within the scope of X.  Comeau online
says that 'overloaded function "X::X" is not a type name', so it
apparently doesn't even get to the issue of access, since it
doesn't even recognize the construction.  This is not an academic
question.  I was trying to create a singleton which, for legacy
reasons, could not be a function (so I couldn't use a Meyers
singleton).

Dave

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by Rob Williscrof » Fri, 01 Aug 2003 21:42:52



 > Consider:
 >
 > struct X
 > {
 >     static X x;
 > private:
 >     X() { }
 > };
 >
 > X::X x;

X X::x;

 >
 > int main() { }
 >

Rob.
--
http://www.victim-prime.dsl.pipex.com/

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by llewell » Fri, 01 Aug 2003 21:43:24



 > Consider:
 >
 > struct X
 > {
 >     static X x;
 > private:
 >     X() { }
 > };
 >
 > X::X x;
[snip]

   X X::x;

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by Sebastian Molesk » Fri, 01 Aug 2003 21:46:16




 > Consider:
 >
 > struct X
 > {
 >     static X x;
 > private:
 >     X() { }
 > };
 >
 > X::X x;

Well, this probably ought to be

X X::x;

 > int main() { }

And now it is legal.

sm

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by James Dennet » Fri, 01 Aug 2003 21:47:28


 > Consider:
 >
 > struct X
 > {
 >     static X x;
 > private:
 >     X() { }
 > };
 >
 > X::X x;

I think you want to declare the static data member x
of class ::X, which has type ::X, so
::X ::X::x;
or more tersely
X X::x;

 > int main() { }
 >
 > Is this legal code?

No.

 > Why or why not?

There is no such thing as X::X.  The class is X, and constructors
do not have names.

 > gcc-3.3 says that X::X() is
 > private, but it seems to me that it should be accessible, since x
 > is declared (and defined?) within the scope of X.

gcc-3.3 isn't giving a great diagnostic there.

 > Comeau online
 > says that 'overloaded function "X::X" is not a type name', so it
 > apparently doesn't even get to the issue of access, since it
 > doesn't even recognize the construction.

Also not a great diagnostic, which is unusual for Comeau.

 > This is not an academic
 > question.  I was trying to create a singleton which, for legacy
 > reasons, could not be a function (so I couldn't use a Meyers
 > singleton).

Hope this helps,

James.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by John Potte » Fri, 01 Aug 2003 21:49:32


On 30 Jul 2003 20:10:30 -0400, "David B. Held"

 > struct X
 > {
 >     static X x;
 > private:
 >     X() { }
 > };

 > X::X x;

The type is X and the object name is X::x.

X X::x;

John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by Friedhelm Hoern » Fri, 01 Aug 2003 21:51:13



 > Consider:
 >
 > struct X
 > {
 >     static X x;
 > private:
 >     X() { }
 > };
 >
 > X::X x;
 >
 > int main() { }
 >
 > Is this legal code? Why or why not?

It isn't:
A class can't contain itself as a member!
otherwise you could write:

X x;       // construction - doesn't matter how...
x;         // access the object
x.x;       // access the member of the object
x.x.x;     // access the member of the member of the object
x.x.x.x;   // access the member of the member of ....

 > gcc-3.3 says that X::X() is
 > private, but it seems to me that it should be accessible, since x
 > is declared (and defined?) within the scope of X.  Comeau online
 > says that 'overloaded function "X::X" is not a type name', so it
 > apparently doesn't even get to the issue of access, since it
 > doesn't even recognize the construction.  This is not an academic
 > question.
[snip]

You can't access a private constructor.
If you declare a constructor private you prevent the compiler from
creating a default (public) constructor and inhibit the access -
therefore you never need to define it (except for the case of in-place
creation in a member function...)

Friedhelm

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by John Mullin » Sat, 02 Aug 2003 00:47:11



> Consider:

> struct X
> {
>     static X x;
> private:
>     X() { }
> };

> X::X x;

> int main() { }

> Is this legal code?  Why or why not?  gcc-3.3 says that X::X()
is
> private, but it seems to me that it should be accessible, since
x
> is declared (and defined?) within the scope of X.  Comeau
online
> says that 'overloaded function "X::X" is not a type name', so
it
> apparently doesn't even get to the issue of access, since it
> doesn't even recognize the construction.  This is not an
academic
> question.  I was trying to create a singleton which, for legacy
> reasons, could not be a function (so I couldn't use a Meyers
> singleton).

    The problem is, as Comeau online tells you is that X::X is
not a type, the correct definition of x is:-

X X::x;

JM

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by Ulrich Eckhard » Sat, 02 Aug 2003 00:51:29



> struct X
> {
>     static X x;
> private:
>     X() { }
> };

> X::X x;

The correct syntax is:
  X X::x;

Uli

--
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by Julien Lam » Sat, 02 Aug 2003 00:53:34



> Consider:

> struct X
> {
>     static X x;
> private:
>     X() { }
> };

> X::X x;

> int main() { }

> Is this legal code?  Why or why not?

I think Comeau's results are correct : the statement
X::X x;
is a declaration only if X::X is a type name. However it is a function
identifier and so you have your error.

Quote:>  This is not an academic
> question.  I was trying to create a singleton which, for legacy
> reasons, could not be a function (so I couldn't use a Meyers
> singleton).

You should have written
X X::x;
^ ^  ^
| |  |____ member x
| |_______ of class X
|_________ having type X

For an alternative to Meyer's form of singleton, you might want to have a
look at Steve Dewhurst's article in C/C++ User Journal's Experts Forum
called "Common knowledge : what are you anyway", in which he describes a
pattern similar to the singleton, called "monospace".

Regards,
Julien

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by David B. Hel » Sat, 02 Aug 2003 00:59:09




Quote:> Consider:

> struct X
> {
>     static X x;
> private:
>     X() { }
> };

> X::X x;

> int main() { }

> Is this legal code?  Why or why not?
> [...]

Daniel Frey politely reminded me that the syntax I really wanted is:

X X::x;

This, of course, is happily accepted by all modern compilers.

Dave

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by B. Kaufma » Sat, 02 Aug 2003 03:09:07



Quote:> Consider:

> struct X
> {
>     static X x;
> private:
>     X() { }
> };

> X::X x;

> int main() { }

> Is this legal code?  

I think this should be:
X X::x;

because I guess you want to define a static member of X named x of type X.

This then compiles fine with comeau online, gcc-3.2.2 and VC 6.0.

Regards,
Ben

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by Francis Glassboro » Sat, 02 Aug 2003 03:09:37




Quote:>Consider:

>struct X
>{
>    static X x;
>private:
>    X() { }
>};

>X::X x;

Well that is certainly an error because there is no nested type X within
either a class(or struct) or namespace called X.

try writing:

X X::x;

Where you but the scope operator makes all the difference

Quote:

>int main() { }

--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow      ACCU

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by llewell » Sat, 02 Aug 2003 09:40:08




>  > Consider:

>  > struct X
>  > {
>  >     static X x;
>  > private:
>  >     X() { }
>  > };

>  > X::X x;

>  > int main() { }

>  > Is this legal code? Why or why not?

> It isn't:
> A class can't contain itself as a member!

That isn't why. It's ill-formed because X::X() is private. The
    definition 'X::X x' is equivalent to 'X x' (which isn't what the
    OP wanted), and would be well-defined if the constructor was
    accessible.

And a class *does* contain its name as a member (see 9/2), though of
    course an object can't contain itself as a member.

Quote:> otherwise you could write:

> X x;       // construction - doesn't matter how...
> x;         // access the object
> x.x;       // access the member of the object
> x.x.x;     // access the member of the member of the object
> x.x.x.x;   // access the member of the member of ....

[snip]

That's not well-formed. But this:

    struct X{};

    X::X x2;
    X::X::X x3;
    X::X::X::X x4;

    is. See 9/2 .

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

static member data + private c'tor

Post by ka.. » Sat, 02 Aug 2003 09:54:18




Quote:> Consider:
> struct X
> {
>     static X x;
> private:
>     X() { }
> };
> X::X x;

What is this line supposed to mean?  There is no type X::X.  Perhaps you
meant to write:

    X   X::x ;

Quote:> int main() { }
> Is this legal code?

Only if you make the change I suggest above.

Quote:> Why or why not?

Why it isn't legal is clear: there is no type X defined in X.

Actually, I'm not 100% sure.  Normally, the name of a class is injected
into the class, so the class X contains a type X, which is itself.  So
it may be legal, but it still isn't very good practice.

Quote:> gcc-3.3 says that X::X() is private, but it seems to me that it should
> be accessible, since x is declared (and defined?) within the scope of
> X.

There is an x declared in X.  But you haven't defined it anywhere.
You've defined an x at global scope.  With type X::X.  If the type
declaration is legal due to class name injection, then the compiler is
right.  The class has no public constructor that can be used to define a
global instance.

Quote:> Comeau online says that 'overloaded function "X::X" is not a type
> name', so it apparently doesn't even get to the issue of access, since
> it doesn't even recognize the construction.

Well, I don't know why it is talking about functions here, but my
tendancy is to agree with it: X::X is not a type name.  But as I say,
I'm not sure.

Quote:> This is not an academic question.  I was trying to create a singleton
> which, for legacy reasons, could not be a function (so I couldn't use
> a Meyers singleton).

You probably want either:
    X       X::x ;
or
    X*      X::x ;

--

Conseils en informatique oriente objet/     http://www.gabi-soft.fr
                    Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]