Hi;

If I do in classes;
class class1;
class class2;

and I make in (class class1) Composite Objects from class class2;.

class class1
{ private:
class2 ** PointerClass2;
public:
PointerClass2 = new class2*[size];
// so I need to put deconstructor to delete (the pointer dynamic memory location)
~ class1 ()
{
delete [] PointerClass2;
PointerClass2 = NULL;
}
}

I understand up code;

but if the compilare do the deconstructor, it go to deconstructor of class2 ,
and class2 don't has ( in data member its) any pointer dynamic memory which need to "delete it".

So;
What do you think of the correct body of deconstructor ( which
must contain .)?


Have you got any idea ?
I have make a lot of tries but with no success ....

1. Use code tags for snippets:
[code=c++] sources

[/code]
2. As usually user defined class name are capitalized.
3. The class1 destructor (not deconstructor ;)) must delete an array of pointers to class2. Formally no need in class2 destructor when you delete an array of pointers to class2, an operator delete [] calls array element destructors for class object elements but pointers are not class objects. However you have (at least) three problems:
- You must initialize PointerClass2 member in class1 constructor or in another members function before class1 destructor call.
- You must explicitly delete all array element pointers if they points to dynamically allocated class2 objects.
- the class class2 must have default constructor (constructor without parameters).
I don't understand why you wrote syntactically incorrect class1 definition. Do you know what's constructor and destructor syntax?

1. Use code tags for snippets:

I am very sorry for that, maybe because when I write, I don't remember any thing only How I explain what I need from the code .:D

2. As usually user defined class name are capitalized.

please. what do you mean with "capitalized".?// what is properties it?

- You must initialize PointerClass2 member in class1 constructor or in another members function before class1 destructor call.

yes, here I did the loop in in class1 constructor which set "NULL" in all array elements.

the class class2 must have default constructor (constructor without parameters).

you means I make 2 constructors ( one is default and next with parameters) ?
because I need the parameters.

Do you know what's constructor and destructor syntax?

yes, like :

public :
class1 (....)
{.....
PointerClass2 = new class2*[size];
}

~ class1 ()
{ delete [] PointerClass2;
 PointerClass2 = NULL; 
}

3.an operator delete [] calls array element destructors for class object elements but pointers are not class objects.

and

- You must explicitly delete all array element pointers if they points to dynamically allocated class2 objects.

you means I do loop ( class1 destructor) to delete all array element pointers.?
I am sorry, maybe I don't understand the above

I don't know what I say in the end but I sure that me thank to you very much.

Ok Let me try to get some order:
You have a line class2 ** PointerClass2; Now that means effectively that you have a set of pointers that point to arrays of objects of type class2. The use of class2 is irrelevant to the problem ( I think).

Now then you put PointerClass2=new class2*[size]; but that is not in a constructor but in the definition of the class so it can't work. Let me assume that you wanted it in a constructor.
Then you wanted to write a delete operator for the class.

As ALWAYS, how you allocated tells you how to delete so I cant really answer your question but let me write a simple class for you. The you can tell us what is correct/wrong with my assumptions and we can go from there.

Note : I have dropped using classXX since it is just too easy to read it incorrectly and get confused. It is not just you that this going to be reading this code.

class A;  // Predeclaration:
class B 
{
 private:
   
    A** Ptr; 

public:

    B(const int arrayLen,const int NArrays) 
       {
          Ptr=new A*[NArrays];
          Ptr[0]=new A[NArrays*arrayLen];
          for(int i=1;i<NArrays;i++)
             Ptr[i]=Ptr[0]+arrayLen*i;   
        }
  
   ~B()  
      {
         delete [] *Ptr;
         delete [] Ptr;
       }
};

Note that that is how you would do it if every array that is stored is the same length. If not then you are going to need an explicit loop in the destructor (and a modified assignment).

This is a first stab at understanding your problem, so you should review it and let us know if it helps/doesn't and explain why not.

1. OK, use tags next time ;)
2. Names: Element (not element), Node (not node) etc. Of course, it's not so good name: Class1. Use more sensible names.
3. Yes, if you define a class constructor, define all others needed too. Some correction to my previous post: strictly speaking, no need in default constructor in your case ( new Class2*[size] ). You must define default Class2 constructor for new Class2[size] expression (see StuXYZ's snippet, for example).

1- I am sorry for writing in a late time.
because yesterday I very busy in an another work.
2- thank you to stab with me; I value to co-production with me;
StuXYZ's
I am understanding your example, but I don't need to specify an element where point?.

in fact;
I do class1 (( the name is: course)) and class2 (((( the name is: student)) .
and made 2 classes inherit from student.
and I made student register in the course.
so I made pointer to pointer in course.

When I wanted to delete the course (( in destructor )).
what can I do ..?
(( I think somethings ..maybe not true))
I need to remove all students in this course.
so, I make in it loop to do in all indexes to put NULL ,
after that delete the pointer.

and in the destructor of student.
I am only put empty body in it.?

Ok : Let me deal with the first and possibly the most important problem.
Inheritance is normally an "is a" relationship. Yes it can be broken but if you do, then you always explain yourself, [either in comments in the code / or when you post and say something about it in you post]

Now students are not course. A student is not a course in almost all normal layouts. So don't inherit.

Certainly a course can contain a number of students. And equally a student can B]contain[/B] courses (normally as pointers but maybe not).

OK that over: It solves your problem.
You could have a class register. That has all the students who are registering. The you have a class teaching. That container all the courses. Now students then get a pointer to a course (or several)
and then courses get an array of student pointers.

Then when you delete both student and course. you do nothing in the destructor.

Note that you will have to delete both register and teaching together, so maybe make them one class. I would also make them in a singleton. (If you are not familiar with singletons don't worry about it now, just create one instance of the class globally. Then it will be deleted on the exit of the program.

Not have another go at writing this. you will have to post a little more code and we will see were you get to.

If you are not familiar with singletons don't worry about it now, just create one instance of the class globally. Then it will be deleted on the exit of the program.

this is good idea, but I can not do that.
because my teacher put some rules , such that: the all class must put it in two files (( .h and .cpp )),
and she do not want use any variable or function in global, because she wants good efficiency.

But I can not know what the best method to delete the object from course (( which contain the pointers from student)) .
and if the compiler come in the destructor of course, it go to the derived's destructor which contain on the same type of that array.

you will have to post a little more code and we will see were you get to.

what are the codes that will help us .?

course:: ~course ()
	{
		delete [] PointerStudents ;
		PointerStudents = NULL ;
	}

maybe do this in loop ....!!!!!

course:: ~course ()
	{
		for ( int i=o ; i< size ; i++ )
		{
		delete [] *PointerStudents ;
		*PointerStudents = NULL ;
		}

and this is in constructor:

course:: course ( int size )
    { 
	 PointerStudents = new student*[size];
		  for (int i =0; i<(size) ; i++ )
		 	  PointerStudents [i] =  NULL ;
     }

thank you ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.