Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to overload the operators new and delete on inherited class in C++

1 Answer

0 votes
#include <iostream>

class Base 
{
	public:
		Base(void) 
		{
		}
		virtual ~Base(void) noexcept
		{
		}
};

class Test : public Base 
{
	public:
		Test(void) : Base() 
		{
		}
		~Test(void) noexcept override 
		{
		}
		void *operator new (size_t) throw () 
		{
			std::cout << "Overloaded operator new" << std::endl;
			return ::new Test;
		}
		void operator delete (void*) noexcept 
		{
			std::cout << "Overloaded operator delete" << std::endl;
		}
};

int main() 
{
	Base *b = new Test();
	delete b;

	return 0;
}

/*
run:

Overloaded operator new
Overloaded operator delete

*/

 



answered Nov 14, 2017 by avibootz
edited Nov 14, 2017 by avibootz

Related questions

1 answer 154 views
1 answer 152 views
152 views asked Mar 23, 2018 by avibootz
1 answer 172 views
2 answers 860 views
1 answer 69 views
1 answer 157 views
1 answer 131 views
...