Topic: How does typedef change things?


Author: damian@cs.monash.edu.au (Damian Conway)
Date: 1997/05/10
Raw View
Steve Clamage <stephen.clamage@eng.sun.com> writes:

>If instead you wrote
> typedef struct { ... } foo;
>you certainly have a valid typedef. "foo" is the name of the type, but
>not the name of the class. You cannot use "foo" to refer to a
>constructor or destructor (and thus you cannot even write them). You
>cannot in this case write "struct foo" for the same reason. The draft
>standard in sections 7.1.3 "The typedef specifier", 9.1 "Class names",
>and 12.1 "Constructors" makes these points explicitly.

Just out of interest, can foo be used as a base class name, i.e.

 class der_foo : public foo { ... };

Taken together, sections 10.1, 9.1 and 7.1.3 would seem to imply you can.

If you _can't_, it might make for a handy idiom for preventing
derivation, although the lack of ctors/dtors might be a nuisance.

damian
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: comeau@panix.com (Greg Comeau)
Date: 1997/05/11
Raw View
In article <5krqr5$j46$1@towncrier.cc.monash.edu.au> damian@cs.monash.edu.au (Damian Conway) writes:
>Steve Clamage <stephen.clamage@eng.sun.com> writes:
>
>>If instead you wrote
>> typedef struct { ... } foo;
>>you certainly have a valid typedef. "foo" is the name of the type, but
>>not the name of the class. You cannot use "foo" to refer to a
>>constructor or destructor (and thus you cannot even write them). You
>>cannot in this case write "struct foo" for the same reason. The draft
>>standard in sections 7.1.3 "The typedef specifier", 9.1 "Class names",
>>and 12.1 "Constructors" makes these points explicitly.
>
>Just out of interest, can foo be used as a base class name, i.e.
>
> class der_foo : public foo { ... };
>
>Taken together, sections 10.1, 9.1 and 7.1.3 would seem to imply you can.

A class does still exist, and foo "refers" to it, so right, you can.


>If you _can't_, it might make for a handy idiom for preventing
>derivation, although the lack of ctors/dtors might be a nuisance.

There's other ways to do that...

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
               Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
 Here:comeau@comeaucomputing.com / BIX:comeau or comeau@bix.com / CIS:72331,3421
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/05/11
Raw View
Damian Conway writes:

> Just out of interest, can foo be used as a base class name, i.e.

Sure it can.  It just cannot be used as an elaborated type specifier
(struct foo or class foo), but it can be used anywhere a class name is
expected.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Joe Halpin" <jhalpin@nortel.ca>
Date: 1997/05/06
Raw View
I'm using a library written by someone else, which has declared a
number of structs using typedef, as in

typedef struct foo {
. . .
};

I'm forward declaring a number of these so I won't have to include a
lot of header files in another header file. However, AIX C++ won't
accept

struct foo;

saying: "foo" was previously declared as "typedef".

My understanding of the standard is that whether a class has been
declared using the 'struct' keyword, or whether 'typedef' has been
used, it's still a type, and can be forward declared. Am I wrong?

Thanks

Joe
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Steve Clamage <stephen.clamage@eng.sun.com>
Date: 1997/05/07
Raw View
Joe Halpin wrote:
>
> I'm using a library written by someone else, which has declared a
> number of structs using typedef, as in
>
> typedef struct foo {
> . . .
> };
>
> I'm forward declaring a number of these so I won't have to include a
> lot of header files in another header file. However, AIX C++ won't
> accept
>
> struct foo;
>
> saying: "foo" was previously declared as "typedef".
>
> My understanding of the standard is that whether a class has been
> declared using the 'struct' keyword, or whether 'typedef' has been
> used, it's still a type, and can be forward declared. Am I wrong?

I'm not sure that
 typedef struct foo { ... };
is valid C++, since no declarator is present. I find section 7
"Declarations" of the draft inconclusive on this point.

In any case, "foo" is the tag which names the class, and the compiler
must not complain about "struct foo;" appearing later if it accepts
the typedef. "foo" is not a typedef name.

If instead you wrote
 typedef struct { ... } foo;
you certainly have a valid typedef. "foo" is the name of the type, but
not the name of the class. You cannot use "foo" to refer to a
constructor or destructor (and thus you cannot even write them). You
cannot in this case write "struct foo" for the same reason. The draft
standard in sections 7.1.3 "The typedef specifier", 9.1 "Class names",
and 12.1 "Constructors" makes these points explicitly.

--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]