c**********e 发帖数: 2007 | 1 class A { virtual void f(int) = 0; };
struct B : private A {
public:
void f(long);
void f(int);
};
Which one of the following statements best describes what is happening in
the code above?
a) "f(int)" is hidden by "f(long)" and cannot be accessed, although it is
overridden.
b) "f(int)" from A cannot be overridden because it is private in A.
Therefore, the code declares two new non-virtual functions.
c) Both "f(int)" and "f(long)" are virtual in B because they share a similar
signature | x***y 发帖数: 633 | | M********5 发帖数: 715 | 3 I think the answer is b. The problem is interesting. | K******g 发帖数: 1870 | 4 b
【在 c**********e 的大作中提到】 : class A { virtual void f(int) = 0; }; : struct B : private A { : public: : void f(long); : void f(int); : }; : Which one of the following statements best describes what is happening in : the code above? : a) "f(int)" is hidden by "f(long)" and cannot be accessed, although it is : overridden.
| x***y 发帖数: 633 | 5 It's d. b is absolutely wrong, as a class can not have two functions with
the same signature. According to C++ rule, when a member function is called,
function match is considered before accessibility, if b is true, then
ambiguity issue will arise.
You can try yourself to define two member functions with the same function
signature in a class, which will lead to compile time error....
【在 M********5 的大作中提到】 : I think the answer is b. The problem is interesting.
| M********5 发帖数: 715 | 6 Yeah. I understand the reason and agree with you now. Thanks!
called,
【在 x***y 的大作中提到】 : It's d. b is absolutely wrong, as a class can not have two functions with : the same signature. According to C++ rule, when a member function is called, : function match is considered before accessibility, if b is true, then : ambiguity issue will arise. : You can try yourself to define two member functions with the same function : signature in a class, which will lead to compile time error....
| o**s 发帖数: 65 | 7 I think it is b.
class C{
public:
void g(long);
void g(int);
};
this one still compiles, so I don't think d is correct. | M********5 发帖数: 715 | 8 these two don't have the same signature.
For the given problem, if you implement those two function, the code will
compile. This means that the code is correct. In that sense, d is correct.
【在 o**s 的大作中提到】 : I think it is b. : class C{ : public: : void g(long); : void g(int); : }; : this one still compiles, so I don't think d is correct.
| o**s 发帖数: 65 | 9 the code compiles because of this:
Therefore, the code declares two new non-virtual functions. | x***y 发帖数: 633 | 10 This statement is wrong, 1. the derived class have all the members of the
base class even if the inheritence is private 2. in a class, the same
function can only appear once, and accesibility won't affect this. As A's :
void f(int) goes into B, you can not have another void f(int) in B.
You can verify yourself in the programme to see that in B void f(int) is
till virtual by creating a class C publicly inheriting from B, and use B* to
point to new C. You can see that void f(int) is virtual.
【在 o**s 的大作中提到】 : the code compiles because of this: : Therefore, the code declares two new non-virtual functions.
| | | o**s 发帖数: 65 | 11 the testcase is correct, b is the right answer.
But for this one:2. in a class, the same
function can only appear once, and accesibility won't affect this. As A's :
void f(int) goes into B, you can not have another void f(int) in B.
I have different opinion.
class A {
void f(int){
cout << "A::f()" << endl;
}
public:
void gg(){
f(10);
}
};
class B : public A {
public:
void f(long);
void f(int){
cout << "B::f() | x***y 发帖数: 633 | 12 In your statement, there are several mistakes that are common for most
people.
First of all, as gg() is defined in A, it can only see void f(int) in A, so
that's why the private comes here. If you want void f(int) in B can be
called by gg(), you need to define virtual void f(int) as virtual, which is
called NVI idiom(please check it yourself).
State again: each function signature must be unqiue in a scope, either in a
class or in a namespace. For the inheritence relationship, there is also
name
【在 o**s 的大作中提到】 : the testcase is correct, b is the right answer. : But for this one:2. in a class, the same : function can only appear once, and accesibility won't affect this. As A's : : void f(int) goes into B, you can not have another void f(int) in B. : I have different opinion. : class A { : void f(int){ : cout << "A::f()" << endl; : } : public:
| o**s 发帖数: 65 | 13 be cool buddy,
1)
First of all, as gg() is defined in A, it can only see void f(int) in A, so
that's why the private comes here. If you want void f(int) in B can be
called by gg(), you need to define virtual void f(int) as virtual, which is
called NVI idiom(please check it yourself).
Yes it is, please look into my program and you will see I am calling gg()
through a B object trying to call the f(int) version in A class, not the
other way around! the output is "A::f()"
2)
State again: each funct | t****t 发帖数: 6806 | 14 b is not correct. d is correct.
but statement such as "a class can not have two functions with the same
signature" is quite vague and has nothing to do with the original question.
the point is, virtual function can always be overriden no matter it is
public or private. option b said the opposite so it is wrong.
so
is
【在 o**s 的大作中提到】 : be cool buddy, : 1) : First of all, as gg() is defined in A, it can only see void f(int) in A, so : that's why the private comes here. If you want void f(int) in B can be : called by gg(), you need to define virtual void f(int) as virtual, which is : called NVI idiom(please check it yourself). : Yes it is, please look into my program and you will see I am calling gg() : through a B object trying to call the f(int) version in A class, not the : other way around! the output is "A::f()" : 2)
| b********e 发帖数: 693 | 15 B is wrong, please see following test case
A *a = new B(). when call a->f(1), it print out B.
class C;
class A {
virtual void f(int) {cout << "A ";};
public:
friend class C;
};
struct B : public A {
public:
void f(long){};
void f(int){cout << "B f";};
};
class C{
public:
void test(){
A *a = new B();
a->f(1);
}
};
【在 c**********e 的大作中提到】 : class A { virtual void f(int) = 0; }; : struct B : private A { : public: : void f(long); : void f(int); : }; : Which one of the following statements best describes what is happening in : the code above? : a) "f(int)" is hidden by "f(long)" and cannot be accessed, although it is : overridden.
| c**********e 发帖数: 2007 | 16 感谢xnxky and thrust两大高手。也感谢参与讨论的诸位。答案是d,解释也与两大高
手的解释一致。
b) "f(int)" from A cannot be overridden because it is private in A.
Therefore, the code declares two new non-virtual functions.
This is incorrect. Whether a member function is private or not does not
play in determining whether or not it can be overridden.
d) "f(int)" is overridden and made public in B. "f(long)" does not hide it.
This is the correct answer. "f(long)" could potentially hide "f(int)" if it
were public in A, B derived publ |
|