|
|
发表于 2006-11-16 09:05:00
|
显示全部楼层
Re:找错,一道华为的面试题
class Base
{
public:
Base() {}
virtual ~Base() {} // should be virual base
};
class A : public Base
{
// should put data members ahead of methods if they are used in the
// rest of class declaration
private:
int * m_pData;
public:
// one constructor is pretty enough, and "explicit" keyword sould be
// used
explicit A(int nSize = 0) : m_pData(NULL) {
// exception is not mandatory, but it's better to have it in constructors
try {
if( nSize ) m_pData = new int[nSize];
} catch (...) {...};
}
~A() { delete []m_pData; }
// because there is a pointer in the class, either you provide copy
// constructor and operator = , or make them private
private:
A(const A &);
A & operator = (const A &);
};
Conclusion
If the correct answer to this quesion is only the "virtual", either you or
the person who wrote this question should Tan JJ Dao Si |
|