- 浏览: 401217 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15345首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1021RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 774It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 972As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1065typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 781There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 830we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 993As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 756There are 3 types of inheritanc ... -
c++ - virtual destructor
2012-09-27 22:01 981As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 1001As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1336in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 944class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 992template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1215There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 861the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 594The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1173In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 877A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1197You 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.1.x86_64.rpm
Linux安装Oracle先决条件gcc-c++-3.4.6-8.x86_64.rpm
gcc-c++-4.4.7-3.el6.i686.rpm
compat-gcc-c++-7.3-2.96.128.i386.rpm
在国产化环境中标麒麟操作系统中,安装软件时提示出现不支持c++编译环境。系统中只能查找到gcc,未找到g++&c++,发现需要重新安装gcc-c++。离线环境无法通过yum命令安装,自己整理了aarch64.rpm离线包。仅供大家参考...
安装oracle11g是提示缺少包,安装gcc-c++-4.4.7-4.el6.x86_64.rpm 后,再次检查不再提示。
compat-gcc-c++-7.3-2.96.122.i386.rpm
适合 redhat enterprise linux 5 上安装
gcc-c++-4.4.6-4.el6.x86_64.rpm
Linux下的gcc,RPM格式,共享资源
Oracle数据库安装在RedHat Linux Enterprise操作系统下compat-gcc-34-c++-3.4.6-4.1.i386.rpm
gcc-c++-4.4.7-4.el6.i686.rpm cpp-4.4.7-4.el6.i686.rpm gcc-4.4.7-4.el6.i686 .rpm libstdc++-devel-4.4.7-4.el6.i686.rpm libstdc++-4.4.7-4.el6.i686.rpm
最全的 C-C++-(9个中文手册).chm。 C++API中文手册,C++中文函数手册,C++chm中文版手册,C++文档,C++参考手册,C++手册总共包含8个chm文件,C++ 库函数大全手册完整版,而且是chm格式带索引、搜索,用着非常方便...
Microsoft Visual C++ 2015-2019 运行库合集是一个重要的软件组件,它包含了从2015年至2019年各个版本的Visual C++运行时环境,旨在确保用户能够运行那些依赖于这些库的Windows应用程序。这个合集解决了在执行某些...
compat-gcc-32-c++-3.2.3-47.3.i386.rpm
gcc-c++-4.4.7-3.el6.x86_64.rpm
compat-gcc-34-c++-3.4.6-19.el6.x86_64.rpm
Microsoft Visual C++ (VC++) 是微软开发的一款重要的编程环境,用于创建Windows平台上的应用程序。它提供了C++编译器和其他工具,使得开发者可以利用C++语言进行高效且强大的软件开发。2015到2019年间的版本,包括...