Topic: [Q] typedef transparensy ?


Author: Ruslan Shevchenko <Ruslan@Shevchenko.Kiev.UA>
Date: 1998/05/24
Raw View
How correct the next code ?

class X
{
};

typedef Y  X;

class  Z: public Y
{};


And next:

class X: public Y
{
   typedef Y Z;
public:
   X()
     :Z()
   {}

};

i. e. in other words: are compiler must eliminate typedef definitions in
symbol table ?



--
    @=
     //RSSH                              mailto:Ruslan@Shevchenko.Kiev.UA

CORBA in Ukraine & ex-USSR: http://www.corbadev.kiev.ua
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/05/25
Raw View
Ruslan Shevchenko <Ruslan@Shevchenko.Kiev.UA> writes:

> class X { };

> typedef Y  X;

you probably mean `typedef X Y;'

> class Z: public Y {};

> And next:

Assuming Y is the class-name of a complete type, this should work:

> class X: public Y { typedef Y Z; public: X() :Z() {} };

The general rule is that typedef-names that refer to classes can be
used as class-names, but not in elaborated-type-specifiers nor as
constructor or destructor names, i.e.:

typedef X W;

struct W x; // ill-formed
W y; // ok

X::W() {} // ill-formed

W::~W() {} // ill-formed

W::~X() {} // ok, IMHO

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 1998/05/26
Raw View
Alexandre Oliva wrote:
...
> The general rule is that typedef-names that refer to classes can be
> used as class-names, but not in elaborated-type-specifiers nor as
> constructor or destructor names, i.e.:
>
> typedef X W;
>
> struct W x; // ill-formed
> W y; // ok
>
> X::W() {} // ill-formed
>
> W::~W() {} // ill-formed
>
> W::~X() {} // ok, IMHO

Where does it say that? Why does it say that? How do you go about
constructing or destroying an object of a class that your code knows
about only through a typedef in someone else's code?


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/05/26
Raw View
In article 41C6@wizard.net, James Kuyper <kuyper@wizard.net> writes:
>Alexandre Oliva wrote:
>....
>> The general rule is that typedef-names that refer to classes can be
>> used as class-names, but not in elaborated-type-specifiers nor as
>> constructor or destructor names, i.e.:
>>
>> typedef X W;
>>
>> struct W x; // ill-formed
>> W y; // ok
>>
>> X::W() {} // ill-formed
>>
>> W::~W() {} // ill-formed
>>
>> W::~X() {} // ok, IMHO
>
>Where does it say that? Why does it say that? How do you go about
>constructing or destroying an object of a class that your code knows
>about only through a typedef in someone else's code?

The rules on typedefs and name lookup say that. See especially
draft standard sections 7.1.3, 12.1, 12.4, and 3.4.

A typedef is an alias for a type. When a typedef name is used, it
resolves to the underlying type. Hence, typedefs are described as
"transparent".

A typedef is not a tag, a "tag" being the label on a class or enum.
Thus, "struct W" above is ill-formed, because W is not a tag name.

The original C++ definition said some things about the names of
constructors and destructors that caused confusion and definitional
problems. The draft standard resolved those problems while retaining
the desired properties by saying that ctors and dtors require a
particular syntax that uses the tag on the class.

Thus, X::W() and X::~W() are ill-formed because W is not a tag.
W::X() and W::~X() are well-formed (although I would not willingly
write code that looked like that) because W is a typename and X
is the tag name associated with it.

If all you have is a typedef name, you can't call the class
destructor explicitly. But you hardly ever need to call a
destructor explicitly. If you need to do so, it is usually
because you are doing things that involve the class
implementation, in which case you have the actual class
definition available.

You don't often need a constructor's name.  Since the typedef
is a type name for the class, that is usually all you need.
(You can use the typedef name in a constructor initializer
list.) Examples:

 W w1(arg1, arg2); // construct W object
 class U : public W { U(); }; // use as base class
 U::U() : W() { }  // initialize base class

---
Steve Clamage, stephen.clamage@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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]