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

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top