Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1103233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:18:42+00:00 2026-05-17T01:18:42+00:00

I have an object say. ClassA *obj1 = new ClassA; ClassA *ptr1 = obj1;

  • 0

I have an object say.

ClassA *obj1 = new ClassA;

ClassA *ptr1 = obj1;
ClassA *ptr2 = obj1;

When I do delete ptr1;, will it affect ptr2? If so what can be the correct solution for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:18:43+00:00Added an answer on May 17, 2026 at 1:18 am

    (assuming obj2 is supposed to be obj1)

    ClassA *x defines a pointer that can point to objects of type ClassA. A pointer isn’t an object itself.

    new ClassA allocates (and constructs) an actual object of type ClassA.

    So the line Class A *obj1 = new ClassA; defines a pointer obj1 and then sets it to point to a newly allocated object of type ClassA.

    The line Class A *ptr1 = obj1; defines a pointer ptr1 and then sets it to point to the same thing obj1 is pointing to, that is, the ClassA object we just created.

    After the line Class A *ptr2 = obj1;, we have three pointers (obj1, ptr1, ptr2) all pointing to the same object.

    If we do delete ptr1; (or equivalently, delete obj1; or delete ptr2;), we destroy the pointed to object. After doing this, any pointer that was pointing to the object is made invalid (which answers your first question: yes, it will affect ptr2 in the sense that ptr2 won’t be pointing to a valid object afterwards).

    The correct solution depends on what you’re trying to achieve:

    • If you want two copies of the object, assuming ClassA has a copy constructor, do ClassA *ptr2 = new ClassA(*obj1);. You will need to delete this new object separately when you are done with it!
    • If you want two pointers to the same object, consider using something like boost::shared_ptr (google it)

    Hmm, that’s alot of text for such a simple q+a. Ah well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a best practices question Let say, I have a class object, like
Say I have a DLL that has the following static/global: ClassA Object; Along with
Say i have a object like this public class Student{ public IList<Coursework> Courseworks{get;set;} public
Let's say I have an object MyObject that looks like this: public class MyObject
I have the following situation: In project A, an object (say Obj1 of class
Let's say I have an object: public class CustomObj { DateTime Date { get;
Let's say I have an object who's class definition looks like: class Command: foo
Let's say I have a generic Object class, and a generic List class. I
Say I have a simple object such as class Something { public int SomeInt
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.