- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is difference between instantiating a C++ object using new vs. without new?
In C++, we can instantiate the class object with or without using the new keyword. If the new keyword is not use, then it is like normal object. This will be stored at the stack section. This will be destroyed when the scope ends. But for the case when we want to allocate the space for the item dynamically, then we can create pointer of that class, and instantiate using new operator.
In C++, the new is used to dynamically allocate memory.
Example
#include <iostream> using namespace std; class Point { int x, y, z; public: Point(int x, int y, int z) { this->x = x; this->y = y; this->z = z; } void display() { cout << "(" << x << ", " << y << ", " << z << ")" << endl; } }; int main() { Point p1(10, 15, 20); p1.display(); Point *ptr; ptr = new Point(50, 60, 70); ptr->display(); }
Output
(10, 15, 20) (50, 60, 70)
- Related Articles
- What is the difference between `new Object()` and object literal notation in JavaScript?
- What is the difference between new operator and object() constructor in JavaScript?
- Difference between "new operator" and "operator new" in C++?
- malloc() vs new() in C/C++
- Virtual vs Sealed vs New vs Abstract in C#
- What is the difference between new/delete and malloc/ free in C/ C++?
- How is a new object created in C#?
- Difference between new and malloc( )
- What is difference between vs. ?
- What is the difference between old style and new style classes in Python?
- Using the new keyword in C#
- When to Use an Alias vs Script vs a New Function in Bash
- What is the difference between the New-AZTag and Update-AZTag command in Azure PowerShell?
- How to create a new directory by using File object in Java?
- What is the difference between String s1 = "Hello" and String s1= new String("Hello") in java?

Advertisements