标签:stream using ini cout 四种 cas str ace 循环
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class base {
public:
virtual void type() {cout << "base" << endl;}
};
class drive : public base {
public:
drive( double d) : dou(d) {}
virtual void type() {cout << "drive" << endl;}
void show() {
cout << dou << endl;
}
private:
double dou;
};
int main() {
shared_ptr<base> b = shared_ptr<base>(new drive(1.2));
shared_ptr<drive> d = dynamic_pointer_cast<drive>(b);
d->show();
}
///写成
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class base {
public:
virtual void type() {cout << "base" << endl;}
virtual void show() {}
};
class drive : public base {
public:
drive( double d) : dou(d) {}
virtual void type() {cout << "drive" << endl;}
virtual void show() {
cout << dou << endl;
}
private:
double dou;
};
int main() {
shared_ptr<base> b = shared_ptr<base>(new drive(1.2));
b->show();
}
《Effective C++》让自己习惯C++:条款26-条款31
标签:stream using ini cout 四种 cas str ace 循环
原文地址:https://www.cnblogs.com/narjaja/p/10187516.html