Online Book Reader

Home Category

Professional C__ - Marc Gregoire [329]

By Root 1386 0
write an overloaded operator new with extra parameters, the compiler will automatically allow the corresponding new-expression. So, you can now write code like this:

MemoryDemo* memp = new(5) MemoryDemo();

delete memp;

Code snippet from Memory\MemoryDemoTest.cpp

The extra arguments to new are passed with function call syntax (as in nothrow new). These extra arguments can be useful for passing various flags or counters to your memory allocation routines. For example, some runtime libraries use this in debug mode to provide the file name and line number where an object is allocated, so when there is a memory leak, the offending line that did the allocation can be identified.

When you define an operator new with extra parameters, you should also define the corresponding operator delete with the same extra parameters. You cannot call this operator delete with extra parameters yourself, but it will be called only when you use your operator new with extra parameters and the constructor of your object throws an exception.

An alternate form of operator delete gives you the size of the memory that should be freed as well as the pointer. Simply declare the prototype for operator delete with an extra size parameter.

If your class declares two identical versions of operator delete except that one takes the size parameter and the other doesn’t, the version without the size parameter will always get called. If you want the version with the size to be used, write only that version.

You can replace operator delete with the version that takes a size for any of the versions of operator delete independently. Here is the MemoryDemo class definition with the first operator delete modified to take the size of the memory to be deleted:

#include

class MemoryDemo

{

public:

MemoryDemo() {}

virtual ~MemoryDemo() {}

void* operator new(std::size_t size);

void operator delete(void* ptr, std::size_t size) noexcept;

// Omitted for brevity

};

Code snippet from Memory\MemoryDemo.h

The implementation of this operator delete calls the global operator delete without the size parameter because there is no global operator delete that takes the size:

void MemoryDemo::operator delete(void* ptr, size_t size) noexcept

{

cout << "operator delete with size" << endl;

::operator delete(ptr);

}

Code snippet from Memory\MemoryDemo.cpp

This capability is useful only if you are writing a complicated memory allocation and deallocation scheme for your classes.

Explicitly Deleting/Defaulting operator new and operator delete

Chapter 6 shows how you can explicitly delete or default a constructor or assignment operator. Explicitly deleting or defaulting is not limited to constructors and assignment operators. For example, the following class deletes the operator new and new[], which means that this class cannot be dynamically created by using new or new[]:

#include

class MyClass

{

public:

void* operator new(std::size_t size) = delete;

void* operator new[](std::size_t size) = delete;

};

Code snippet from Memory\ExplicitDelete\ExplicitDelete.cpp

Using this class in the following ways will result in compiler errors:

int main()

{

MyClass* p1 = new MyClass;

MyClass* pArray = new MyClass[2];

return 0;

}

Code snippet from Memory\ExplicitDelete\ExplicitDelete.cpp

SUMMARY


This chapter summarized the rationale for operator overloading and provided examples and explanations for overloading the various categories of operators. We hope that this chapter taught you to appreciate the power that it gives you.

Throughout this book, operator overloading is used to provide abstractions, including iterators in Chapter 17 and smart pointers in Chapter 21.

The next chapter starts an in-depth discussion of another advanced C++ feature called templates.

Chapter 19

Writing Generic Code with Templates


WHAT’S IN THIS CHAPTER?

How to write template classes

How the compiler processes templates

How to organize template source code

How to use non-type template parameters

How to write templates of individual class

Return Main Page Previous Page Next Page

®Online Book Reader