How can I initialize a type pointer * using type () syntax?

Built-in type variables can be initialized by value as follows:

int var = int(); 

that way, I get the default value int without hardcoding zero in my code.

However, if I try to do similar stuff for a pointer:

 int* ptr = int*(); 

the compiler (Visual C ++ 10) refuses to compile this (says type int unexpected ).

How do I initialize a pointer in the same way?

+14
c ++ pointers initialization local-variables built-in-types
Nov 09 '11 at 15:47
source share
7 answers

How do I initialize a Type * pointer using type () syntax?

You can not. The T() syntax is defined in 5.2.3 / 1,2 (C ++ 03, a slightly different wording in C ++ 11 FDIS). In particular, the second paragraph says:

The expression T (), where T is a specifier of a simple type (7.1.5.2) for an object type without an array or a type (void cv-qualified) void, creates an rvalue of the specified type, which is initialized with the value (8.5);

This means that int() will create an rvalue of type int and value-initializes it. Now the problem is that int* not a simple type specifier, but rather a specified type specifier. The definition of a simple type specifier in a grammar:

  simple-type-specifier: ::opt nested-name-specifieropt type-name ::opt nested-name-specifier template template-id char wchar_t bool short int long signed unsigned float double void 

A type name is defined as:

  type-name: class-name enum-name typedef-name 

This is what makes the proposed solutions work. Creating a typedef (either directly or through a template) creates a type name (third type), which can be used as a specifier of a simple type (first type).

+4
Nov 09 '11 at 16:35
source share

Use typedef to create a name for your pointer type:

 typedef int *ip; ip ptr = ip(); 

The same idea should work for other types that require more than one lexical element (word) to determine the type name (for example, unsigned long , long long , unsigned long long * , etc.).

+9
Nov 09 '11 at 15:54
source share

The use of typedef not required (but exclusively for C ++ 11), which can be useful when working with several types of pointers:

 template<typename T> using alias = T; 

then alias<int*> is int* , so you can do int* p = alias<int*>() .

A similar solution available for C ++ 03 using identification metadata:

 template<typename T> struct identity { typedef T type; }; int* p = identity<int*>::type(); 
+3
Nov 09 '11 at 16:08
source share

Just do

 int* x = int(); 

It still assigns 0 to the pointer, forcing it to point to a NULL address, since you are using it by default with a default value of 0.

In fact, it works great for all types:

 double* y = int(); 

The address is still a 32-bit (or 64-bit depending on the platform) integer, so I think it should work fine for all types if you do = int ().

+1
Nov 09 '11 at 15:53
source share

Here is one way:

 template <typename T> T make_default() { return T(); } int main() { int *p = make_default<int*>(); } 
+1
Nov 09 '11 at 15:53
source share

The reason this does not work is because pointers do not have constructors.

Syntax

int ()

causes (theoretically)

int::int ()

which initializes the variable. (Most likely, the compiler simply zeros the variable, but this is because it “knows” about ints).

In C ++ 11 you can use nullptr

int *ptr = nullptr;

Otherwise, you pretty much have to use NULL:

int *ptr = NULL;

0
Nov 09 '11 at 15:55
source share

This is how you do it int * ptr = new int;

Heap Pointers

-one
Nov 09 '11 at 15:54
source share



All Articles