- 浏览: 389202 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
Let's see what if we want to clone some thing when virtual and class hierarchy is considered.
// it is ok if we know in advance what is the type of object we are cloning. NotQuery *pnq; NotQuery *pnq2 = new NotQuery(*pnq); // however what if the object type we don't know const Query* pq = pnq->op() ; // suppose that pnq-> returns some Query instance. const Query * pq3 = new ???(*pq);
So, why not create anthor function which is called clone, which is virtual and each derived class know how to create it self.
so we have the following code.
class Query { public: virtual Query* clone() = 0; }; class NameQuery : public Query { public: NameQuery(string name) : _name(name) { } NameQuery(const NameQuery &rhs) : _name(rhs._name) {} virtual Query* clone() // invokes the NameQuery constructor { return new NameQuery(*this); } private: string _name; };
You see in this impl, we have a clone method which return Query * pointer. and you will probably use the code as below.
void test_virtually_virtual_new() { Query *pq = new NameQuery("Rike"); // so this is not working as the // Query::clone() returns Qeury //NameQuery * pq2 = pq->clone(); NameQuery *pnq2 = static_cast<NameQuery *> (pq->clone()); // but why this is needed of the static cast? NameQuery *pnq3 = static_cast<NameQuery *>(pnq2->clone()); }
this is not neat, expecially when we already have a NameQuery poiter, we have to do the downcast...
But remember we have discussed there is an exception to the function signature mismatch, where virtual function can have different return types in the base and in the derived class.
Here is how we reimplemented it.
class NameQuery : public Query { public: NameQuery(string name) : _name(name) { } NameQuery(const NameQuery &rhs) : _name(rhs._name) {} //virtual Query* clone() // // invokes the NameQuery constructor //{ return new NameQuery(*this); } // while the following is what you should do virtual NameQuery *clone() { return new NameQuery(*this); } private: string _name; };
Now, we can use the virtual new as follow.
void test_virtually_virtual_new_return_overloaded() { Query *pq = new NameQuery("Rike"); NameQuery* pnq2 = static_cast<NameQuery *> (pq->clone()); // there is no need to do the static_cast as you saw before. // NameQuery* pnq3 = pnq2->clone(); }
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15284首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 964RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 744It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 919As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1031typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 735There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 783we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 913As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 721There are 3 types of inheritanc ... -
c++ - virtual destructor
2012-09-27 22:01 924As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 950As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1286in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 903class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 957template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1181There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 802the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 553The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1133In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 837A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1126You can overload the subscript ...
相关推荐
warning: gcc-c++-4.4.7-3.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY error: Failed dependencies: gcc = 4.4.7-3.el6 is needed by gcc-c++-4.4.7-3.el6.x86_64 libstdc++ = 4.4.7-...
gcc-c++-3.4.6-3.i386.rpm
gcc-c++-3.4.6-3.1.x86_64.rpm
Linux安装Oracle先决条件gcc-c++-3.4.6-8.x86_64.rpm
gcc-c++-4.4.7-3.el6.i686.rpm
gcc-c++-4.4.6-4.el6.x86_64.rpm
compat-gcc-c++-7.3-2.96.128.i386.rpm
C++ 内存布局虚继承 ---Empty virtual base classs (空虚基类).doc
安装oracle11g是提示缺少包,安装gcc-c++-4.4.7-4.el6.x86_64.rpm 后,再次检查不再提示。
compat-gcc-c++-7.3-2.96.122.i386.rpm
gcc-c++-4.1.2-54.el5.x86_64.rpm
适合 redhat enterprise linux 5 上安装
Linux下的gcc,RPM格式,共享资源
mysql-connector-c++-noinstall-1.0.5-win32-vs2005.zip
Oracle数据库安装在RedHat Linux Enterprise操作系统下compat-gcc-34-c++-3.4.6-4.1.i386.rpm
compat-gcc-34-c++-3.4.6-19.el6.x86_64.rpm
gcc-c++-4.4.7-3.el6.x86_64.rpm
compat-gcc-32-c++-3.2.3-47.3.i386.rpm