
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Difference between "new operator" and "operator new" in C++?
In C++ when we want to create a new object, we have to create a memory block into the memory, then also call constructor to initialize the memory block. We can create memory element by using the new keyword. This new operator is doing two consecutive task. But the operator new just only create memory space.
New Keyword
The new operator is a special type of operator, which denotes a request for memory allocation on the heap section. When sufficient memory is available, then only new operators initializes the memory to the pointer variable. When we create an object using a normal new keyword it performs two consecutive tasks
- ? Allocate memory block for the object into the heap section
- ? Call the constructor to initialize the memory block.
Example Code
#include <iostream> using namespace std; class Box { public: Box() { cout << "Constructor called!" <<endl; } }; int main() { Box* myBox = new Box; }
Output
Constructor called!
Operator New
The operator new is a function. This function is used to overload the new operator. Using this we can allocate raw memory block. This is conceptually similar to the malloc() or calloc() functions. This function does not call the constructor so memory is not initialized. However after returning the pointer from this overloaded method, the compiler calls the constructor after that.
Example Code
#include <iostream> #include <cstdlib> using namespace std; class Box { public: Box() { cout << "Constructor called!" <<endl; } void *operator new(size_t size) { cout << "Call Operator New"<<endl; void *p = malloc(size); return p; } }; int main() { Box* myBox = new Box; }
Output
Call Operator New Constructor called!
- Related Articles
- What is the difference between new operator and object() constructor in JavaScript?
- new and delete operator in C++
- Placement new operator in C++
- The new operator in Java
- The new operator in JavaScript
- Creating JavaScript constructor using the “new” operator?
- What is 'new' Operator in JavaScript?
- Create class Objects in Java using new operator
- Difference between == and === operator in JavaScript
- Difference between !== and ==! operator in PHP
- How to create JavaScript objects using new operator?
- Difference between the Ternary operator and Null coalescing operator in php
- Difference between new and malloc( )
- Difference between concat() and + operator in Java
- Difference between == and is operator in python.
