`

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 ...

    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 ...

    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 ...

    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 ...

    HTML5 Mastery: Semantics, Standards, and Styling

    - The Drag and Drop API simplifies the implementation of drag-and-drop interactions, enhancing user engagement and interaction. 6. **Becoming Skilled in Contemporary Web Standards** - Staying ...

    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 ...

    Microsoft Excel 2019 VBA and Macros

    Use this guide to automate virtually any routine Excel task: save yourself hours, days, maybe even weeks. Make Excel do things you thought were impossible, discover macro techniques you won’t find ...

Global site tag (gtag.js) - Google Analytics