0

I am still learning C++ and still not very proficient.

Say, I have a class like this?

class Parent
{
public:
// Destructor
~Parent() noexcept;

private:
struct Child
{
   Child* other;
};

Child* firstCell;
Child* lastCell;

};

What is the proper, modern, and safe way to write a destructor?

Is it just delete Child::other? or, delete Child->other?

Wndrlnd
  • 1
  • 1
  • `Child` is a class that, outside of being nested within `Parent`, has absolutely no relationship to `Parent`. If a `Parent` stores an object of type `Child`, it will only be because it has a member of that type. It's not clear what you're trying to accomplish here, since there is no object of type `Child` to get an `other` from. – Nicol Bolas Apr 28 '20 at 23:03
  • I am trying to create something a cell that keeps track of all of its neighbors, for example. About, the smart pointer question that has been linked; I understand that I can use a smart pointer that has its own destructor, thereby passing on the problem to std pointers. However, what if I have to use a raw pointer? – Wndrlnd Apr 28 '20 at 23:11
  • To elaborate on my cell example and why I have a class inside -- The ```Parent``` class is like a grid or matrix and the struct inside will be the individual cells. – Wndrlnd Apr 28 '20 at 23:16
  • That still doesn't answer my question. You don't have any `Child` *objects*, and if there are no `Child` objects, then there's nothing for `Parent` to reference and destroy. So what are you asking about? – Nicol Bolas Apr 28 '20 at 23:27
  • I might be missing something. But, in my understanding, I thought there will be private member variables like ```Child* firstCell```, ```Child* lastCell```, etc. inside the class. Does that answer the question? Sorry I am really new to C++. – Wndrlnd Apr 28 '20 at 23:39
  • They may be there in your code, but they aren't there in the code you *posted*, so I cannot see them. – Nicol Bolas Apr 29 '20 at 00:03

0 Answers0