Why does compiler not give warning for this code

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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top