`

c++ - c++ exception to return type match and virtually virtual new operator

    博客分类:
  • c++
c++ 
阅读更多

How do we duplicate a object through its pointer or reference? suppose 

 

NotQuery *pnq;

NotQuery *pnq2 = new NotQuery(*pnq);

 

 

however, what if we have this ?

 

 

const Query *pq = pnq->op();

// ???

 

 

we cannot dclare a virutal new instance of operator new, the new operator is a static member, and it applies before the constructor. 

 

THINK:??? you can overload new operator inside the class, , however, we cannot make it virtual still it does not answer the time/order problem.

 

the solution is a surrogate, somehting like this:

 

 

public Query {
public:
   virtual Query * clone() = 0;
};
 

 

however, if we cannot change the return type in the derived class, then we might see this

 

 

NameQuery *png = new NameQuery("Rike");
NameQuery *pnq2 = static_cast<NameQuery*> (pnq->clone());

 

BUT!!!,there is an exception to that "requirement that return type of the derived class must match exactly that of the base class".

 

 

so, in the derived class, you can write 

 

public NameQuery : public Query 
{
public:
  virtual NameQuery* clone() { return new NameQuery(*this); }
};

 

 

Below shows the whole code ::

 

class Base 
{
public :
	Base() { }
	Base(Base &base) {}
	virtual Base *clone();
protected:
	Base *print();
private:

};

class Derived : public Base {
public:
	Derived() {}
	Derived (Derived &derived) : Base(derived) {} 
	virtual Derived *clone();
protected:
	Base *print();

};

Base* Base::clone(){ 
	return new Base(*this);
}

Derived* Derived::clone() {
	return new Derived(*this);
}
 

 

分享到:
评论

相关推荐

    acpi控制笔记本风扇转速

    operand to be an operation region of any type. It is now restricted to regions of type SystemMemory, as per the ACPI specification. BZ 481 Additional cleanup and optimizations for the new Table ...

    Modern C++ Design: Generic Programming and Design Patterns

    Displaying extraordinary creativity and virtuosity, Alexandrescu offers a cutting-edge approach to software design that unites design patterns, generic programming, and C++, enabling programmers to ...

    C++17 Standard Library Quick Reference A Pocket Guide to Data Structures

    The C++ Standard Library is a collection of essential classes and functions used by millions of C++ programmers on a daily basis. Being part of the ISO Standard of the C++ Programming Language, an ...

    Complete Virtual Reality and Augmented Reality Development with Unity-2019.epub

    Complete Virtual Reality and Augmented Reality Development with Unity by Jesse Glover Jonathan Linowes Packt Publishing English 2019-04-17 668 pages Details Title: Complete Virtual Reality and ...

    accelerated c++

    Based on the authors' intensive summer C++ courses at Stanford University, Accelerated C++ covers virtually every concept that most professional C++ programmers will ever use -- but it turns the '...

    Modern C++ Design

    In Modern C++ Design, Andrei Alexandrescu opens new vistas for C++ programmers. Displaying extraordinary creativity and virtuosity, Alexandrescu offers a cutting-edge approach to software design that ...

    Wrox.Ivor.Horton's.Beginning.Visual.C++.2012

    Offering a comprehensive introduction to both the standard C++ language and to Visual C++, he offers step-by-step programming exercises, examples, and solutions to deftly guide novice programmers ...

    Unity Virtual Reality Projects.pdf

    Chapter 1, Virtually Everything for Everyone, is an introduction to the new technologies and opportunities in consumer virtual reality (VR) as regards games and non-gaming applications. Chapter 2, ...

    Tusers.Security.Component.v2.0.0.3.For.BCB6.Cracked

    The latest version of our security components for Delphi and C++ Builder allows the developer to easily add password protection and access control to resources like Buttons, Menus, DBGrids, TFields ...

    Addison.Wesley.C.Plus.Plus.Coding.Standards.101.Rules.Guidelines.and.Best.Practices.Oct.2004.eBoo.chm

    From type definition to error handling, this book presents C++ best practices, including some that have only recently been identified and standardized-techniques you may not know even if you've used ...

    RabbitMQ in Action

    DESCRIPTION There's a virtual switchboard at the heart of most large applications, where millions of messages and requests need to be routed to and from the servers, programs, and services that make ...

    RabbitMQ In Action

    DESCRIPTION There's a virtual switchboard at the heart of most large applications, where millions of messages and requests need to be routed to and from the servers, programs, and services that make ...

    RxLib控件包内含RxGIF,全部源码及DEMO

    TFormStorage allows you to read and write virtually any component published property to an INI file or the system Registry with virtually no code. Works with 3rd party and your own custom controls as ...

    RabbitMQ in action

    DESCRIPTION There's a virtual switchboard at the heart of most large applications, where millions of messages and requests need to be routed to and from the servers, programs, and services that make ...

    Cellular Internet of Things_Technologies, Standards, and Performance.pdf

    To support, and perhaps further boost, this growth the Third Generation Partnership Project (3GPP) standards development organization has in its Release 13 developed three new technologies known as ...

    Hadoop 2 Quick-Start Guide_ Lea - Douglas Eadline

    With Hadoop 2.x and YARN, Hadoop moves beyond MapReduce to become practical for virtually any type of data processing. Hadoop 2.x and the Data Lake concept represent a radical shift away from ...

Global site tag (gtag.js) - Google Analytics