C++ GOTCHA 3
1. Sometimes the following code seems to work just fine. Other times it crashes mysteriously. At other times the system seems sluggish. Why?
class Table
{
public:
Table(int sz);
...
private:
int *memory;
int size;
...
};
Table::Table(int sz):size(sz),memory(new int[size])
{
...
}
int main()
{
Table t(128);
return 0;
}
2. which f is called in the code below, and why?
class C
{
public:
int f(char *) { return 3; }
float f(int *) { return 3.14159; }
};
int main()
{
C x;
float g;
g = x.f(0); // what happens?
cout << g << endl;
return 0;
}
3. Explain the compiler error message generated by this code:
struct S {
int &ref;
S();
};
int main()
{
S x,y;
x = y; // illegal, why?
return 0;
}