C++ virtual constructors virtual copy constructors 以及non-memeber functions的行为虚化

 

  C++中,所谓的 virtual constructors是某种函数,视其获得的输入,可产生不同类型的对象。-----《more effective c++》

  virtual constructors 在许多情况下有要有用,其中之一就是从磁盘读取对象信息。

 

   下面是一个小实例,比如,我在项目的根目录下,新建一个文本文件 a1.txt,文件中的内容是(Derone,Derother),我现在要实现的功能就是,可以根据文件中的内容,调用相应类中的方法。

先看一下代码,再继续分析

代码如下:

//virtual constructors,virtual copy constructors #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; //Base class class Base { public: Base(const string& s); virtual Base* clone() const = 0; //virtual copy constructors virtual ostream& printf(ostream& s) const =0; protected: string str; }; Base::Base(const std::string &s):str(s) { } //Derone class class Derone : public Base { public: Derone(const string& s); Derone* clone() const;//virtual copy constructors ostream& printf(ostream& s) const; }; Derone::Derone(const std::string &s) :Base(s) { cout<<"调用Derone's construct"<<endl; } Derone* Derone::clone() const { return new Derone(*this); } ostream& Derone::printf(ostream& s) const { s<<str<<endl; return s; //cout<<str<<endl; } class Derother : public Base { public: Derother(const string& s); Derother* clone() const; //virtual copy constructors ostream& printf(ostream& s) const; }; Derother::Derother(const std::string &s) :Base(s) { cout<<"调用Derother's construct"<<endl; } Derother* Derother::clone() const { return new Derother(*this); } ostream& Derother::printf(ostream& s) const { s<<str<<endl; return s; } inline ostream& operator << (ostream &s,const Base& c) //non-member functions 的行为虚化 { return c.printf(s); } //User class class User { public: User(istream& str); User(const User& rhs); static Base* readcomponet(const string& str); void print(User& a) const; private: vector<Base*> ivec; //类型为基类的指针Base* string objname; }; User::User(istream& str) { string s; while(str>>s) { ivec.push_back(readcomponet(s)); //读取文件中的内容,根据不同的内容,产生不同的对象 } } User::User(const User &rhs) { //迭代遍历rhs的vector,运用每个元素的virtual copy constructor(clone), //将元素复制到此对象的vector中 for(vector<Base*>::const_iterator iter = rhs.ivec.begin(); iter != rhs.ivec.end(); ++iter) { if((*iter) != NULL)//如果是空指针,就不添加对象 { ivec.push_back((*iter)->clone()); } } } Base* User::readcomponet(const std::string &str) { if(str == "Derone") { return new Derone("Derone"); } if(str == "Derother") { return new Derother("Derother"); } return NULL; //如果文件中的内容不符合要求,返回空指针。 } void User::print(User& a) const { for(vector<Base*>::iterator iter = a.ivec.begin(); iter != a.ivec.end(); ++iter) { if(*iter != NULL) { //(*iter)->printf(); cout<<*(*iter)<<endl; } } } //打开文件函数 ifstream& open_file(ifstream &in,const string &file) { in.close(); in.clear(); in.open(file.c_str()); if(!in) { cout<<"open file fail"<<endl; } return in; } int main() { string filename = "a1.txt"; string str; ifstream in; open_file(in,filename); User a(in); User b(a); a.print(a); b.print(b); system("pause"); return 0; }

 

代码虽然有点长,但是很简单,首先,从main()函数开始,调用open_file()函数,打开文件,就返回读文件流 in.

然后, readcomponet()函数,实现了 virtual constructor 函数的功能,他根据读入文件内容的不同,返回不同类型的指针,并将其添加到vector 容器里,容器的类型是Base*(基类指针),因此C++多态机制就发挥作用了。

 

 

 

你可能感兴趣的:(C++,String,vector,user,iterator,Constructor)