Kai-Uwe Bux
11/26/2008 3:35:00 AM
abhisheksaksena1@gmail.com wrote:
> I have following code:-
>
> template<class T>
> struct type
> {
> typedef T X;
> type ():first(T()){}
> T first; // the first stored value
> };
>
> struct coordinate
> {
> coordinate(unsigned i, unsigned j):x(i), y(j){}
> unsigned x,y;
> };
>
> int main ()
> {
>
> type<coordinate *> p;
>
> coordinate *third= p.first; //<---pointer third is initialized to
> zero
> }
>
> How come here p.first is initialized to zero?
Because it is initialized by the value of the temporary T() where T is
coordinate*.
> What T() for pointer means while initializing first?
As per [5.2.3/2]:
The expression T(), where T is a simple-type-specifier (7.1.5.2) for a
non-array complete object type or the (possibly cv-qualified) void type,
creates an rvalue of the specified type, which is value-initialized
(8.5; no initialization is done for the void() case).
the pointer is value-initialized. For scalar types, such as coordinate*,
this means initialization by 0.
> Does operator () defined for pointers?
No.
Best
Kai-Uwe Bux