Class继承:静态成员和虚方法
Class inheritance: static members and virtual methods
我正在做一个例外 class,它只是在 cout
上报告问题并退出程序,如下所示:
class Exception {
protected:
short code;
string text;
public:
friend ostream& operator <<(ostream& out, const Exception& p_exception) {
return out << p_exception.text;
}
void execute() { cout << text; exit(code);
};
具体例子:
class IndexOutOfBoundsException : public Exception {
public:
IndexOutOfBoundsException() {
this->text = "\nERR: An unsuccessful attempt was made to access the index outside the bounds of the array!";
code = 1;
}
};
class IndexOfEmptyFieldException : public Exception {
public:
IndexOfEmptyFieldException() {
this->text = "\nERR: An unsuccessful attempt was made to access the index of an empty field!";
code = 2;
}
};
class AllocationFailureException : public Exception {
public:
AllocationFailureException() {
this->text = "\nERR: An unsuccessful attempt was made to allocate dynamic memory!";
code = 3;
}
};
在我看来,这一切看起来都非常整洁,但现在它似乎不是一个很好的 OOP 示例。当我仔细考虑时,我突然想到我可以以某种方式使用静态成员,比如使 int code;
成为特定于继承 classes 的静态变量。或者,我可以使方法 void generate();
成为带有 = 0
的纯虚函数,这是我的第一个想法。
我的问题是:是否有可能使此解决方案成为更好的 OOP 示例and/or我是否遗漏了 OOD 的一般要点?
这是一个示例,它减少了对象占用空间并将内存分配从 throw 子句移开:
class Exception {
protected:
short code;
const string &text;
Exception(short code, const string &text) :
code(code), text(text)
{}
...
}
class IndexOutOfBoundsException : public Exception {
private:
static const string c_text = "\nERR: An unsuccessful attempt was made to access the index outside the bounds of the array!";
public:
IndexOutOfBoundsException() : Exception(1, c_text)
{ }
};
我正在做一个例外 class,它只是在 cout
上报告问题并退出程序,如下所示:
class Exception {
protected:
short code;
string text;
public:
friend ostream& operator <<(ostream& out, const Exception& p_exception) {
return out << p_exception.text;
}
void execute() { cout << text; exit(code);
};
具体例子:
class IndexOutOfBoundsException : public Exception {
public:
IndexOutOfBoundsException() {
this->text = "\nERR: An unsuccessful attempt was made to access the index outside the bounds of the array!";
code = 1;
}
};
class IndexOfEmptyFieldException : public Exception {
public:
IndexOfEmptyFieldException() {
this->text = "\nERR: An unsuccessful attempt was made to access the index of an empty field!";
code = 2;
}
};
class AllocationFailureException : public Exception {
public:
AllocationFailureException() {
this->text = "\nERR: An unsuccessful attempt was made to allocate dynamic memory!";
code = 3;
}
};
在我看来,这一切看起来都非常整洁,但现在它似乎不是一个很好的 OOP 示例。当我仔细考虑时,我突然想到我可以以某种方式使用静态成员,比如使 int code;
成为特定于继承 classes 的静态变量。或者,我可以使方法 void generate();
成为带有 = 0
的纯虚函数,这是我的第一个想法。
我的问题是:是否有可能使此解决方案成为更好的 OOP 示例and/or我是否遗漏了 OOD 的一般要点?
这是一个示例,它减少了对象占用空间并将内存分配从 throw 子句移开:
class Exception {
protected:
short code;
const string &text;
Exception(short code, const string &text) :
code(code), text(text)
{}
...
}
class IndexOutOfBoundsException : public Exception {
private:
static const string c_text = "\nERR: An unsuccessful attempt was made to access the index outside the bounds of the array!";
public:
IndexOutOfBoundsException() : Exception(1, c_text)
{ }
};