由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - [c++]distinct default parameter value in inherited class
相关主题
有谁读过Design Patterns Explained: A New Perspective on Obj (转载)ajax小问题
类设计问题求助请问C++小白问题
问一到算法题C++ Q96: function inheritance (转载)
问两个算法题, 没什么头绪question about using Hive parameter (转载)
inheritence problemWhy oop is bad?
两个看来相似的问题c++ template question:
protected class member in C++一道Microsoft的面试题
Default function template argumentsInterview question for Quant to share-1, please discuss and help each other
相关话题的讨论汇总
话题: draw话题: shapecolor话题: red话题: class话题: public
进入Programming版参与讨论
1 (共1页)
G****A
发帖数: 4160
1
The question is inspired by Item 38 in <>. Given
following code,
*****************
enum ShapeColor { RED, GREEN, BLUE };
class Shape {
public:
virtual void draw(ShapeColor color = RED) const = 0;
...
};
class Rectangle: public Shape {
public:
//definition of draw() goes here.
...
};
********************
How to define the draw() in Rectangle so that it comes with a default
parameter value other than RED?
NOTE: the article did mention that a definition like this will not work:
**********
virtual void draw(ShapeColor color = GREEN) const;
**********
P********e
发帖数: 2610
2
不记得ec++说什么了,但是,default argument不是function signature,所以
override的时候, base/derived可以有不同default argument value 。
virtual除了function definition是动态绑定,其他都是静态绑定,包括这个value.
所以在用pointer->draw()的时候,灭有办法确定 draw() 在name lookup时候,一定是
GREEN。

【在 G****A 的大作中提到】
: The question is inspired by Item 38 in <>. Given
: following code,
: *****************
: enum ShapeColor { RED, GREEN, BLUE };
: class Shape {
: public:
: virtual void draw(ShapeColor color = RED) const = 0;
: ...
: };
: class Rectangle: public Shape {

r*******y
发帖数: 1081
3
it is interesting. I also write a simple code to check
#include
using namespace std;
class I{
public:
virtual void f(int i = 1){cout << i < };
class A: public I{
public:
void f(int j = 2){cout << j << endl;}
};
int main(){
I *p = new A;
p->f();
}
For this code, I think the result would have been 2
but in fact it is 1.

【在 G****A 的大作中提到】
: The question is inspired by Item 38 in <>. Given
: following code,
: *****************
: enum ShapeColor { RED, GREEN, BLUE };
: class Shape {
: public:
: virtual void draw(ShapeColor color = RED) const = 0;
: ...
: };
: class Rectangle: public Shape {

1 (共1页)
进入Programming版参与讨论
相关主题
Interview question for Quant to share-1, please discuss and help each otherinheritence problem
stl quiz 一问两个看来相似的问题
interview questionsprotected class member in C++
请教一个简单字符串程序 (转载)Default function template arguments
有谁读过Design Patterns Explained: A New Perspective on Obj (转载)ajax小问题
类设计问题求助请问C++小白问题
问一到算法题C++ Q96: function inheritance (转载)
问两个算法题, 没什么头绪question about using Hive parameter (转载)
相关话题的讨论汇总
话题: draw话题: shapecolor话题: red话题: class话题: public