c**********e 发帖数: 2007 | 1 #include
using namespace std;
class Base {
protected:
virtual void output(std::ostream& os) { os << "Base\n"; }
};
class Derived : private Base {
friend std::ostream& operator<<(std::ostream&, Derived&);
};
std::ostream& operator<<(std::ostream& os, Derived& d) {
d.output(os);
}
int main() {
Base b; Derived d;
std::cout << b << d;
}
For which one of the following reasons is the code above INVALID?
a) Base::output is defined for Base, but operator<< is defined for Derived.
b) |
P*******b 发帖数: 1001 | 2 c
【在 c**********e 的大作中提到】 : #include : using namespace std; : class Base { : protected: : virtual void output(std::ostream& os) { os << "Base\n"; } : }; : class Derived : private Base { : friend std::ostream& operator<<(std::ostream&, Derived&); : }; : std::ostream& operator<<(std::ostream& os, Derived& d) {
|
P*******b 发帖数: 1001 | 3 c)
【在 c**********e 的大作中提到】 : #include : using namespace std; : class Base { : protected: : virtual void output(std::ostream& os) { os << "Base\n"; } : }; : class Derived : private Base { : friend std::ostream& operator<<(std::ostream&, Derived&); : }; : std::ostream& operator<<(std::ostream& os, Derived& d) {
|
M********5 发帖数: 715 | |
l*******o 发帖数: 791 | |
c**********e 发帖数: 2007 | 6 You guys are all right.
c) There is no operator<< defined for Base.
This is the correct answer. The code will fail to compile because you try
to output a Base object, and there is no corresponding operator<<. |