您的位置:首页 > 编程语言 > C语言/C++

C++基础--------带默认参数的虚函数 静态类型和动态类型分析

2013-09-06 01:07 549 查看
// 字串查找.cpp : 定义控制台应用程序的入口点。   
//   
#include "stdafx.h"   
#include <iostream>   
#include <complex>   
using namespace std;  
class base{  
public:  
    base(){  
        cout<<"base::base()\n";  
    }  
    ~base(){  
cout<<"base::~base()\n";  
    }  
    virtual void f(int){  
        cout<<"base::f(int)\n";  
    }  
    virtual void f(double){  
        cout<<"base::f(double)\n";  
    }  
    virtual void g(int i=10){  
        cout<<"base::g()"<<i<<endl;  
    }  
};  
  
class derived:public base{  
public:  
    derived(){  
        cout<<"derived::derived()\n";  
    }  
    ~derived(){  
        cout<<"derived::~derived()\n";  
    }  
    void f(complex<double> c){  
        cout<<"derived:f(complex)\n";  
    }  
    virtual void g(int i=20){  
        cout<<"derived::g()"<<i<<endl;  
    }  
};  
int _tmain(int argc, _TCHAR* argv[])  
{  
    base b;  
    derived d;  
    base *pb=new derived;  
    cout<<sizeof(base)<<endl;  
    cout<<sizeof(derived)<<endl;  
    pb->f(1.0);  
    pb->g();  
    getchar();  
    return 0;  
}  

// 字串查找.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <complex>
using namespace std;
class base{
public:
base(){
cout<<"base::base()\n";
}
~base(){
cout<<"base::~base()\n";
}
virtual void f(int){
cout<<"base::f(int)\n";
}
virtual void f(double){
cout<<"base::f(double)\n";
}
virtual void g(int i=10){
cout<<"base::g()"<<i<<endl;
}
};

class derived:public base{
public:
derived(){
cout<<"derived::derived()\n";
}
~derived(){
cout<<"derived::~derived()\n";
}
void f(complex<double> c){
cout<<"derived:f(complex)\n";
}
virtual void g(int i=20){
cout<<"derived::g()"<<i<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
base b;
derived d;
base *pb=new derived;
cout<<sizeof(base)<<endl;
cout<<sizeof(derived)<<endl;
pb->f(1.0);
pb->g();
getchar();
return 0;
}




pb->g(); 在base和derived都定义了含有相同参数列表的g(),因此这里发生多态。pb指针指向的是derived类的对象,因此调用的是derived类的g().这里注意,由于参数值是在编译期就已经决定的,因此参数i应该取base类的默认值,即10

 

例2:

[cpp]
view plaincopyprint?

#include <iostream>   
#include <complex>   
using namespace std;  
class A{  
public:  
    virtual void its(const char* const str="A"){  
        cout << "This is A str=" << str << endl;  
    }  
    static void show(A & obj){  
        obj.its();  
    }  
};  
  
class B : public A{  
public:  
    virtual void its(const char* const str="B"){  
        cout << "This is B str=" << str << endl;  
    }  
};  
  
class C : public A{  
public:  
    virtual void its(const char* const str="C"){  
        cout<<"This is C str="<<str<<endl;  
    }  
};  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    C cc;  
    B bb;  
    A::show(cc);  
    A::show(bb);  
    bb.its();  
    cc.its();  
    int i=2;  
    const int &a=i;  
    getchar();  
    return 0;  
}  

#include <iostream>
#include <complex>
using namespace std;
class A{
public:
virtual void its(const char* const str="A"){
cout << "This is A str=" << str << endl;
}
static void show(A & obj){
obj.its();
}
};

class B : public A{
public:
virtual void its(const char* const str="B"){
cout << "This is B str=" << str << endl;
}
};

class C : public A{
public:
virtual void its(const char* const str="C"){
cout<<"This is C str="<<str<<endl;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
C cc;
B bb;
A::show(cc);
A::show(bb);
bb.its();
cc.its();
int i=2;
const int &a=i;
getchar();
return 0;
}




默认参数在编译的时候已经写死了,不会动态的。

例3:

[cpp]
view plaincopyprint?

#include "stdafx.h"   
#include <iostream>   
#include <complex>   
using namespace std;  
enum ShapeColor{RED,GREEN,BLUE};  
//一个表示集合形状的类   
class Shape{  
public:  
    //所有的形状都要提供一个函数憨直它们本身   
    virtual void draw(ShapeColor color=RED) const =0;  
};  
  
class Rectangle:public Shape{  
public:  
    //定义了不同的缺省参数值   
    virtual void draw(ShapeColor color=GREEN) const{  
        cout<<"Rectangle::draw "<<color<<endl;  
    }  
protected:  
private:  
};  
class Circle:public Shape{  
public:  
    virtual void draw(ShapeColor color) const{  
    cout<<"Circle::draw "<<color<<endl;  
    }  
protected:  
private:  
};  
int _tmain(int argc, _TCHAR* argv[])  
{  
    //ps,pc,pr都被声明为Shape指针类型,所以他们都以此作为自己的静态类型。   
    Shape *ps,*pc,*pr;  
    pc=new Circle;  //静态类型=Shape*
  
    pc->draw(BLUE);  
    pr=new Rectangle;//静态类型=Shape*
  
    pr->draw();  
    getchar();  
    return 0;  
}  

#include "stdafx.h"
#include <iostream>
#include <complex>
using namespace std;
enum ShapeColor{RED,GREEN,BLUE};
//一个表示集合形状的类
class Shape{
public:
//所有的形状都要提供一个函数憨直它们本身
virtual void draw(ShapeColor color=RED) const =0;
};

class Rectangle:public Shape{
public:
//定义了不同的缺省参数值
virtual void draw(ShapeColor color=GREEN) const{
cout<<"Rectangle::draw "<<color<<endl;
}
protected:
private:
};
class Circle:public Shape{
public:
virtual void draw(ShapeColor color) const{
cout<<"Circle::draw "<<color<<endl;
}
protected:
private:
};
int _tmain(int argc, _TCHAR* argv[])
{
//ps,pc,pr都被声明为Shape指针类型,所以他们都以此作为自己的静态类型。
Shape *ps,*pc,*pr;
pc=new Circle;  //静态类型=Shape*
pc->draw(BLUE);
pr=new Rectangle;//静态类型=Shape*
pr->draw();
getchar();
return 0;
}




Effective C++ (这个貌似是第三版前的,不知道哪个版本,从电子书上搞下来的,3rd是条款37)

条款38: 决不要重新定义继承而来的缺省参数值

让我们从一开始就把问题简化。缺省参数只能作为函数的一部分而存在;另外,只有两种函数可以继承:虚函数和非虚函数。因此,重定义缺省参数值的唯一方法是重定义一个继承而来的函数。然而,重定义继承而来的非虚函数是一种错误(参见条款37),所以,我们完全可以把讨论的范围缩小为 "继承一个有缺省参数值的虚函数" 的情况。

既然如此,本条款的理由就变得非常明显:虚函数是动态绑定而缺省参数值是静态绑定的。

什么意思?你可能会说你不懂这些最新的面向对象术语;或者,过度劳累的你一时想不起静态和动态绑定的区别。那么,让我们来复习一下。

对象的静态类型是指你声明的存在于程序代码文本中的类型。看下面这个类层次结构:

enum ShapeColor { RED, GREEN, BLUE };

// 一个表示几何形状的类

class Shape {

public:

  // 所有的形状都要提供一个函数绘制它们本身

  virtual void draw(ShapeColor color = RED) const = 0;

  ...

};

class Rectangle: public Shape {

public:

  // 注意:定义了不同的缺省参数值 ---- 不好!

  virtual void draw(ShapeColor color = GREEN) const;

  ...

};

class Circle: public Shape {

public:

  virtual void draw(ShapeColor color) const;

  ...

};

用图形来表示是下面这样:

  Shape

  /\

  / \

  / \

  Rectangle Circle

现在看看这些指针:

Shape *ps; // 静态类型 = Shape*

Shape *pc = new Circle; // 静态类型 = Shape*

Shape *pr = new Rectangle; // 静态类型 = Shape*

这个例子中, ps, pc,和pr都被声明为Shape指针类型,所以它们都以此作为自己的静态类型。注意,这和它们真的所指向的对象的类型绝对没有关系 ---- 它们的静态类型总是Shape*。

对象的动态类型是由它当前所指的对象的类型决定的。即,对象的动态类型表示它将执行何种行为。上面的例子中,pc的动态类型是Circle*,pr的动态类型是Rectangle*。至于ps,实际上没有动态类型,因为它(还)没有指向任何对象。

动态类型,顾名思义,可以在程序运行时改变,典型的方法是通过赋值:

ps = pc; // ps的动态类型

  // 现在是Circle*

ps = pr; // ps的动态类型

  // 现在是Rectangle*

虚函数是动态绑定的,意思是说,虚函数通过哪个对象被调用,具体被调用的函数就由那个对象的动态类型决定:

pc->draw(RED); // 调用Circle::draw(RED)

pr->draw(RED); // 调用Rectangle::draw(RED)

我知道这些都是老掉牙的知识了,你当然也了解虚函数。(如果想知道它们是怎么实现的,参见条款M24)但是,将虚函数和缺省参数值结合起来分析就会产生问题,因为,如上所述,虚函数是动态绑定的,但缺省参数是静态绑定的。这意味着你最终可能调用的是一个定义在派生类,但使用了基类中的缺省参数值的虚函数:

pr->draw(); // 调用Rectangle::draw(RED)!

这种情况下,pr的动态类型是Rectangle*,所以Rectangle的虚函数被调用 ---- 正如我们所期望的那样。Rectangle::draw中,缺省参数值是GREEN。但是,由于pr的静态类型是Shape*,这个函数调用的参数值是从Shape类中取得的,而不是Rectangle类!所以结果将十分奇怪并且出人意料,因为这个调用包含了Shape和Rectangle类中Draw的声明的组合。你当然不希望自己的软件以这种方式运行啦;至少,用户不希望这样,相信我。

不用说,ps, pc,和pr都是指针的事实和产生问题的原因无关。如果它们是引用,问题也会继续存在。问题仅仅出在,draw是一个虚函数,并且它的一个缺省参数在子类中被重新定义了。

为什么C++坚持这种有违常规的做法呢?答案和运行效率有关。如果缺省参数值被动态绑定,编译器就必须想办法为虚函数在运行时确定合适的缺省值,这将比现在采用的在编译阶段确定缺省值的机制更慢更复杂。做出这种选择是想求得速度上的提高和实现上的简便,所以大家现在才能感受得到程序运行的高效;当然,如果忽视了本条款的建议,就会带来混乱。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多态