Блог в котором есть много интересной информации…
У них тест состоит из четырех простых вопросов
Перепишите код, устранив имеющиеся в нём проблемы, но не изменяя функцию main
class Foo
{
public:
Foo(int j) { i=new int[j]; }
~Foo() { delete i; }
private:
int* i;
};
class Bar: Foo
{
public:
Bar(int j) { i=new char[j]; }
~Bar() { delete i; }
private:
char* i;
};
void main()
{
Foo* f=new Foo(100);
Foo* b=new Bar(200);
*f=*b;
delete f;
delete b;
}
Надо добавить конструктор по умолчанию, виртуальный деструктор, публичное наследование, длину массива и оператор присваивания.
class Foo
{
public:
Foo() : i(0), len(-1) {}
Foo(int j) { i=new int[j]; len = j;}
virtual ~Foo()
{
//it works fine even if i==0
delete i;
}
Foo & operator = (const Foo & right)
{
delete i;
//propably we need to implement some kind of reference counting
//but I simply made a copy of the array
if (right.i == 0)
{
i = 0;
len = -1;
}
else
{
len = right.len;
i = new int[len];
memcpy(i, right.i, len * sizeof(int));
}
return *this;
}
private:
int* i;
int len;
};
class Bar: public Foo
{
public:
Bar(int j) { i=new char[j]; }
~Bar() { delete i; }
private:
char* i;
};
void main()
{
Foo* f=new Foo(100);
Foo* b=new Bar(200);
*f=*b;
delete f;
delete b;
}
std::wstring GetWindowTextW(HWND hWnd)
{
int len = GetWindowTextLength(hWnd);
std::wstring s;
s.reserve(len + 1);
::GetWindowTextW(hWnd, const_cast<wchar_t*>(s.c_str()), len);
return s;
}