T
Tommy Andreasen
Hi,
I have the following code:
--------------------------------------
#include <iostream>
class MyObj
{
public:
MyObj()
{
std::cout << this << " MyObj constructor called\n";
}
MyObj(const MyObj&)
{
std::cout << this << " MyObj copy constructor called\n";
}
~MyObj()
{
std::cout << this << " MyObj destructor called\n";
}
operator bool()
{
return true;
}
bool isOK()
{
return true;
}
};
class Iterator
{
public:
MyObj getNext() const
{
return MyObj();
};
};
int main(int, char**)
{
Iterator iter;
while (MyObj m = iter.getNext())
{
if (m.isOK())
break;
}
return 0;
}
-------------------------------------------
When i run the program, I get the following output:
0012FF54 MyObj constructor called
0012FF54 MyObj destructor called
0012FF54 MyObj destructor called
Looks like the destructor is called twice, once after the break
statement and once after leaving the scope of the while loop.
Tommy -
tommy(dot)andreasen(at)radiometer(dot)dk
I have the following code:
--------------------------------------
#include <iostream>
class MyObj
{
public:
MyObj()
{
std::cout << this << " MyObj constructor called\n";
}
MyObj(const MyObj&)
{
std::cout << this << " MyObj copy constructor called\n";
}
~MyObj()
{
std::cout << this << " MyObj destructor called\n";
}
operator bool()
{
return true;
}
bool isOK()
{
return true;
}
};
class Iterator
{
public:
MyObj getNext() const
{
return MyObj();
};
};
int main(int, char**)
{
Iterator iter;
while (MyObj m = iter.getNext())
{
if (m.isOK())
break;
}
return 0;
}
-------------------------------------------
When i run the program, I get the following output:
0012FF54 MyObj constructor called
0012FF54 MyObj destructor called
0012FF54 MyObj destructor called
Looks like the destructor is called twice, once after the break
statement and once after leaving the scope of the while loop.
Tommy -
tommy(dot)andreasen(at)radiometer(dot)dk