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 8599447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:24:00+00:00 2026-06-12T01:24:00+00:00

So say I have foo* fooObject1 = new foo[10]; // here would go code

  • 0

So say I have

foo* fooObject1 = new foo[10];
// here would go code for initializing fooObject1[0] to fooObject1[9] with a foo object for each.
foo* fooObject2 = new foo[30];

for(int = 0; i < 10; i++)
fooObject2[i] = fooObject1[i];

if(fooObject1 != NULL)
delete[] fooObject1; //this "destroys" the array, BUT ALSO calls the destructors of all the foo objects inside fooObject1, causing fooObject2 elements to be anything but what it was before.

fooObject1 = fooObject2;

where foo is a specific object created by me with its respective methods.

My problem here, however, is that I want to destroy the array that fooObject1 points to, but not the elements of the array, is there a way for that to be done in C++? I know another way to go around the problem is to overload the = (equals) operator, but in the program I need it for, writing that would include overloading many other = operators for many other classes, which makes it a tedious and long process.
I was hoping that in some way I could keep the objects in fooObject2, but get rid of the array pointed by fooObject1. So can this be done? How? If not how, then are there are websites you could redirect me to read about it?

  • 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-06-12T01:24:01+00:00Added an answer on June 12, 2026 at 1:24 am

    These are independent. Your fooObject2 array has been allocated just as fooObject1 was, and assignment operators fired for each all along the way. you effectively have a copy of each object from fooObject1 in fooObject2.

    you cannot abandon fooObject1. You need to delete[] it or you’ll leak memory. fooObject2, as written, is independent unless you have internal pointers in the fooObject class and a shallow copy was performed because you didn’t follow the Rule of Three.

    The last line of this sets fooObject1 (the pointer) to point to the same objects as fooObject2 after you cleaned up fooObject1 and deleted its prior memory allocation, just FYI, but do NOT delete them BOTH after that reference is made; only delete ONE.

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

Sidebar

Related Questions

Let's say I have a Foo.fsx script that contains this code: module Bar =
Let say I have a WCF Foo(int param); The client is passing in a
Say I have 2 files: foo bar baz and 123 456 f[want autocomplete here]
Say I have some foo :: Maybe Int and I want to bind it
Not sure what I'm doing wrong here, but say I have: foo.h class foo
Say I have a class foo with an object of class bar as a
Say we have void Foo() { var bar = new Bar(); bar.Woo(); } Will
Say you have the following: foo(x: String)(y: Int): Int foo(x: String)(y: Double): Int Scala
Say I have the following: Foo* foo = new Foo(bar); //later on *foo =
Say we have: public void foo() { someRefType test = new someRefType (); test

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.