- Joined
- Oct 7, 2010
- Messages
- 1
- Reaction score
- 0
The complier does not give any error for the below code. As the statement X obj definition is only conditionally executed, the compiler should give an error to ensure guaranteed initialization using constructor. But it does not. Please provide your views.
struct X
{
int i;
int j;
public:
int add(){return i+j;}
void init(){i=10;j=20;}
};
int main()
{
int i;
i=10;
if (i==10)
{
goto label;
}
X obj;
label:
cout<<"Compiler error";
obj.init();
cout<<obj.add();
}
struct X
{
int i;
int j;
public:
int add(){return i+j;}
void init(){i=10;j=20;}
};
int main()
{
int i;
i=10;
if (i==10)
{
goto label;
}
X obj;
label:
cout<<"Compiler error";
obj.init();
cout<<obj.add();
}