1

I had been getting myself confused on a few concepts lately, so I wanted to be sure I know the proper way to answer these questions, here is what I got

class Bar
{
    // stuff here
};

class Foo
{
    private:
        Bar* bar;
        Bar* arr;

    public:
        void setBar(Bar*);
        void setArr(Bar*, int);
        Foo();
};

Foo::Foo()
{
    arr = new Bar[10];
}


void Foo::setBar(Bar* bar)
{
    this->bar = bar;
}

void Foo::setArr(Bar* bar, int element)
{
    // i did not test this to see if it worked so if i have incorrect syntax, im meaning to have an allocated array of Bar pointer objects 
    this->arr[element] = bar;
}


int main()
{
    Foo foo();

    Bar* bar = new Bar();

    foo.setBar(bar);

    Bar* bar2 = new Bar();

    foo.setArr(bar2, 4);

}

1) If I do this command in main, "delete bar", does it delete the reference to bar inside the foo class ?

2) same goes for "delete bar2" does it delete the reference to bar object inside the allcoated array inside the foo class ?

3) sInce i declared Foo as Foo foo() instead of Foo* foo = new Foo(), since it is not an allocated object, i cant delete it(far as I know), so can any of the references within the class ever go out of scope ?

4) I think i may have gotton the wrong syntax since I did this on notepad, how could I assign an allocated array of Bar objects, of Bar pointers ? so each array element of arr is a Bar* that I can delete later in the deconstructor

5) lets say I did do Foo* foo = new Foo();, if i delete foo, im guess the references remain unless I take care of them in constructor?

I think that is all the questions I had left, I just needed some clarification, thanks in advance guys and gals!

Tanner Summers
  • 689
  • 1
  • 8
  • 26
  • Can you explain the line `this->arr[element] = bar`? I asked this because both `arr` and `bar` have type `Bar *`, hence/but `arr[element]` has type `Bar` instead of `Bar *`. Did you received any error? – DMaster Nov 15 '15 at 03:59
  • @Dmaster I did not type it in a IDE, so I could not have tested it at the moment, that is one of the reasons I also wanted to as kit on the side on what I did wrong lol, I was trying to show what happens when you have a dynamically allocated array of Bar* objects, but I think i made the mistake and dynamically allocated an array of Bar objects, not bar*, and with that said, I want to know how to actually do that :) thank you for the reply – Tanner Summers Nov 15 '15 at 04:01
  • Still, I think you should correct that line, I compiled your code by my compiler, it told me error. `this->arr[element] = *bar` instead of `this->arr[element] = bar` would be correct. Also, I'll tell you alternative of keyword `this`, when you need to get member, instead of `this->membername`, you can write just `membername`, and oh, I see *namesake*, you can use `classname::membername` (i.e. `Foo::bar = bar` and `arr[element] = *bar` instead of `this->bar = bar` and `this->arr[element] = *bar]`), anyway, that's your choice. – DMaster Nov 15 '15 at 04:15
  • thank you for your advice and help :) – Tanner Summers Nov 15 '15 at 04:17

1 Answers1

3

Question:

1) If I do this command in main, "delete bar", does it delete the reference to bar inside the foo class ?

Yes. The danger is that now the foo object has a dangling pointer.

Question:

2) same goes for "delete bar2" does it delete the reference to bar object inside the allcoated array inside the foo class ?

Same thing. Yes. The danger is that now the foo object has a dangling pointer.

Question:

3) sInce i declared Foo as Foo foo() instead of Foo* foo = new Foo(), since it is not an allocated object, i cant delete it(far as I know), so can any of the references within the class ever go out of scope ?

The line

Foo foo();

does not create an object. It declares a function foo that takes no arguments and returns a Foo. See What is the purpose of the Most Vexing Parse? for more details.

To create an object, use:

Foo foo;

or

Foo foo{};

Coming to your question, the resources used by the object are released by calling the destructor when the object goes out of scope. In this case, the object goes out of scope when the program returns from main.

Question:

4 ...... how could I assign an allocated array of Bar objects, of Bar pointers ? so each array element of arr is a Bar* that I can delete later in the deconstructor

With the arr declared as type Bar*, each element of arr can be an object, i.e. a Bar, not a pointer. You can use:

arr[0] = Bar{};

You can't use:

arr[0] = new Bar;

Question:

5) lets say I did do Foo* foo = new Foo();, if i delete foo, im guess the references remain unless I take care of them in constructor?

That does not make sense. I think you meant to say "unless I take care of them in constructor?"

That would be correct.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @R Sahu Thank you for your reply, let me add to it. for questions 1&2 thank you, for 3, i meant to do it as declaring an object then calling constructor so Foo foo; is what I wanted, i am not sure why I thought id make it also call the constructor by doing Foo foo();. with that said, what is the difference between Foo foo; and Foo foo{};? to also add, for question 4, how can I make it so it will be arr[0] = new Bar();? for question 5, yes, I meant, if an object had references, and I did deleted the object without deleting the reference objects within that class, would they remain in memory – Tanner Summers Nov 15 '15 at 04:06
  • @TannerSummers, `Foo foo;` is called default initialization while `Foo foo{};` is called value initialization. Head over to http://en.cppreference.com/w/cpp/language/initialization to understand various initialization methods. – R Sahu Nov 15 '15 at 04:10
  • 1
    @TannerSummers, to use `arr[0] = new Bar();`, `arr` has to be an array of pointers. You'll need to declare it as `Bar** arr;` and define it as `arr = new Bar*[10];`. – R Sahu Nov 15 '15 at 04:12
  • @TannerSummers, you are welcome. I am glad I was able to help. – R Sahu Nov 15 '15 at 04:17
  • @R Sahu, i just thought of one more question, can the same concept be allowed to if a class, allocates an array and returns it to main, if the class gets deleted, which i have set up to call the deconstructor and delete the allocated memory of the array in the class, im guess since that array was returned to main, that it will no longer be accessible in main? – Tanner Summers Nov 15 '15 at 16:36
  • @TannerSummers, It will be OK if the object is allocated using stack memory. It would be problematic if the object is allocated using heap memory. If you delete the object and try to access the elements of the array after the object is deleted,then you will see undefined behavior. – R Sahu Nov 15 '15 at 18:28