Default pointer initialization

Hi,

when writing a template I sometimes need to initialize an attribute of a given template parameter type. This may be any class or even a pointer type. F.e.:

1
2
3
4
5
6
7
8
9
10
11
template<typename T> class A
{
private:
    T _a;

public:
    A() :
        _a()
    {}

} /* A */;

I think this works well in case of f.e.:

 
A<string> a;

creating a with an empty string _a. But does the initializer of line 8 set _a to NULL if T is of any pointer type as f.e. in:

 
A<string *> a;
No. However, it does initialise built in types such as int.
Yes, it does. It has to otherwise generic programming is broken.

1
2
3
4
5
6
7
8
template< typename T >
class A
{
    T a;

  public:
     A() : a() {}
};


sets a to 0 if T = int, 0.0 if T = double, NULL if T = pointer-to-anything, false if T = bool, 0 if T is of any enum type.



Last edited on
Quite right!
Topic archived. No new replies allowed.