hi folks, I need help on how to answer the following questions. Could someone kindly help. thanks.
Q1
struct Test
{
Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{
Test a(1);
a.fun();
Test b();
b.fun();
}
The above program generates a compilation error
"error C2228: left of '.fun' must have class/struct/union " at "b.fun();". Why?
--------------
Q2
struct CLS
{
int m_i;
CLS( int i ) : m_i(i) {}
CLS() {CLS(0);}
};
CLS obj1(1);
cout << obj1.m_i << endl;
CLS obj2;
cout << obj2.m_i << endl;
The output of the above program is
1
-858993460
So why does "CLS obj2;" fail to initialize obj2.m_i as 0?
----------------
Q3
float a = 1.0;
cout << (int&)a << endl;
The output of the above program is
1065353216
Why?
My guess is that (int&)a is interpreting the bit pattern of the float number a as an integer. Am I correct?
----------------
Q4
vector<int> array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
// delete all 2's
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
if( 2 == *itor ) array.erase( itor );
}
The above program generates a run-time error after removal of first 2. Looks like "++itor" is causing the error. What went wrong?
Q1
struct Test
{
Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{
Test a(1);
a.fun();
Test b();
b.fun();
}
The above program generates a compilation error
"error C2228: left of '.fun' must have class/struct/union " at "b.fun();". Why?
--------------
Q2
struct CLS
{
int m_i;
CLS( int i ) : m_i(i) {}
CLS() {CLS(0);}
};
CLS obj1(1);
cout << obj1.m_i << endl;
CLS obj2;
cout << obj2.m_i << endl;
The output of the above program is
1
-858993460
So why does "CLS obj2;" fail to initialize obj2.m_i as 0?
----------------
Q3
float a = 1.0;
cout << (int&)a << endl;
The output of the above program is
1065353216
Why?
My guess is that (int&)a is interpreting the bit pattern of the float number a as an integer. Am I correct?
----------------
Q4
vector<int> array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
// delete all 2's
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
if( 2 == *itor ) array.erase( itor );
}
The above program generates a run-time error after removal of first 2. Looks like "++itor" is causing the error. What went wrong?