Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello.

Please observe the following code :) :
int i1;         // initialized by a "random" value
int i2 = int(); // initialized by 0


Could you explain me the effect of the second line, please ? :-D

Is it an type operator or a "constructor" ?

(
Discovered by :) :
template <typename T>
class holder
{
  T m_value;
public:
  holder() { m_value = T(); }; // ...wow (!)
...
};

)

Thank you !
Posted
Updated 4-May-10 7:08am
v2

It's a bit like a cast and works outside of templates - see here[^].
 
Share this answer
 
The second sample can be also
C++
template <typename T>
class holder
{
  T m_value;
public:
  holder() :m_value() {}
...
};


Any way ... that's standard C++: calling a type like it is a function creates a temporary object constructed with the passed parameters.
Hence:
C++
T myval;
...
myval = T();

just creates a temporary T constructed explicitly with its default constructor and then assigned to myval;

For built-in constructors are called only explicitly and default constructors just zero the memory, hence int() is a temporary int set to 0.

Your first example is based on that concept, but it is a little different:
despite on what it looks, int i2 = x is not an assignment, but an "initialization by copy".

It's like if int::int(const int&) is called to initialize i2, using a temporary int constructed with int::int() (the default constructor), whose code does { memset(this,0,sizeof(int)); }

In your case, you'd probably better to simply declare int i2=0;.

The code you see is a workaround for generic algorithms, that works for a generic T type, to be sure that the T default constructor is always called even in the case that T is a built-in type (C++ don't do automatic call of default constructor for built-in types initialization, to retain backward compatibility with C)
 
Share this answer
 
Hmmm... :)
It works like a fuction,
that creates a temp instance filled by 0,
now like a "smart" constructor... :-D

For the given code:
typedef struct {
  int left,
      top,
      right,
      bottom;
} rect;
  
{
  rect r = {0};             // first 
  r = rect();               // second
  memset(&r, 0, sizeof(r)); // third
}

...wir have the following sequence :) :
ASM
rect r = {0};
00A414E1 C7 45 C4 00 00 00 00 mov         dword ptr [ebp-3Ch],0
00A414E8 33 C0                xor         eax,eax
00A414EA 89 45 C8             mov         dword ptr [ebp-38h],eax
00A414ED 89 45 CC             mov         dword ptr [ebp-34h],eax
00A414F0 89 45 D0             mov         dword ptr [ebp-30h],eax
  r = rect();
00A414F3 33 C0                xor         eax,eax                  // temp begin
00A414F5 89 85 C8 FE FF FF    mov         dword ptr [ebp-138h],eax
00A414FB 89 85 CC FE FF FF    mov         dword ptr [ebp-134h],eax
00A41501 89 85 D0 FE FF FF    mov         dword ptr [ebp-130h],eax
00A41507 89 85 D4 FE FF FF    mov         dword ptr [ebp-12Ch],eax // temp end
00A4150D 8B 8D C8 FE FF FF    mov         ecx,dword ptr [ebp-138h]
00A41513 89 4D C4             mov         dword ptr [ebp-3Ch],ecx
00A41516 8B 95 CC FE FF FF    mov         edx,dword ptr [ebp-134h]
00A4151C 89 55 C8             mov         dword ptr [ebp-38h],edx
00A4151F 8B 85 D0 FE FF FF    mov         eax,dword ptr [ebp-130h]
00A41525 89 45 CC             mov         dword ptr [ebp-34h],eax
00A41528 8B 8D D4 FE FF FF    mov         ecx,dword ptr [ebp-12Ch]
00A4152E 89 4D D0             mov         dword ptr [ebp-30h],ecx
  memset(&r, 0, sizeof(r));
00A41531 6A 10                push        10h
00A41533 6A 00                push        0
00A41535 8D 45 C4             lea         eax,[ebp-3Ch]
00A41538 50                   push        eax
00A41539 E8 ED FC FF FF       call        @ILT+550(_memset) (0A4122Bh)
00A4153E 83 C4 0C             add         esp,0Ch

This is a debug-sequence, may be not optimized...
...but it is very useful, I think... :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900