The object is deleted. Technically speaking, the pointer is a variable that holds the memory address of a certain type, and is statically allocated in the mentioned example. Therefore, it does not require deletion. There is a way to dynamically create pointers, which we have not covered in the course. Great question!
in the last portion of Lecture 6 it is mentioned that with primitive data type pointers (int, double...etc) the "object" which the pointer points to is automatically created with the pointer, does this mean that if a class has a pointer ,let's assume of type integer, we don't need to override the class constructor and destructor? (no dynamic allocation and deallocation is needed?)
Even though the rule still applies when you have a pointer to a primitive type as member of a class, eventually the copy constructor will have to be overridden to achieve proper member copying, which involves dynamically creating a primitive type with new. Therefore, an object created using the copy constructor will not delete the dynamically primitive type if its destructor is the default destructor. This makes overriding the destructor to deal with the possibility of a dynamically primitive member a must. But having a destructor which deletes primitive types as if they were dynamically allocated necessitates that we make sure that the default constructor creates the primitive type dynamically. See? It's all set up. That's a very deep and penetrating question. Keep them coming!
You need to use the copy constructor of the base class in the initialization and pass the object being copied to it. The object will get sliced and treated as a base class type. The following code illustrates. Good question.
#include
using namespace std;
class A { public:int i; }; class B:public A { public:int k; B(){}; B (const B & og):A (og), k (og.k) { }; };
WOULD YOU PLEASE POST SOME PRACTICING PROBLEM?
ReplyDeleteTHANK U.
Try to practice on the problems we covered in the lab.
DeleteThe Statement : "delete Pa ;" from the code in lecture 6
ReplyDeleteis it means Deletion for the object the pointer points to & pointer also
or just deletion for the dynamically allocated object ? without pointer
The object is deleted. Technically speaking, the pointer is a variable that holds the memory address of a certain type, and is statically allocated in the mentioned example. Therefore, it does not require deletion. There is a way to dynamically create pointers, which we have not covered in the course. Great question!
Deletein the last portion of Lecture 6 it is mentioned that with primitive data type pointers (int, double...etc) the "object" which the pointer points to is automatically created with the pointer, does this mean that if a class has a pointer ,let's assume of type integer, we don't need to override the class constructor and destructor? (no dynamic allocation and deallocation is needed?)
ReplyDeleteEven though the rule still applies when you have a pointer to a primitive type as member of a class, eventually the copy constructor will have to be overridden to achieve proper member copying, which involves dynamically creating a primitive type with new. Therefore, an object created using the copy constructor will not delete the dynamically primitive type if its destructor is the default destructor. This makes overriding the destructor to deal with the possibility of a dynamically primitive member a must. But having a destructor which deletes primitive types as if they were dynamically allocated necessitates that we make sure that the default constructor creates the primitive type dynamically. See? It's all set up.
DeleteThat's a very deep and penetrating question. Keep them coming!
In the Concept of inheritance do we concern the situation when our base class is a deep class or not ?
ReplyDeleteThey are unrelated topics. If you have a deep class in the context of inheritance, just do what it takes for deep classes to work properly.
DeleteThis comment has been removed by the author.
ReplyDeletewhen we have deep class inherits a deep class , how should we built the copy constructor ? should we use the initialization list ?
ReplyDeleteYou need to use the copy constructor of the base class in the initialization and pass the object being copied to it. The object will get sliced and treated as a base class type. The following code illustrates. Good question.
Delete#include
using namespace std;
class A
{
public:int i;
};
class B:public A
{
public:int k; B(){};
B (const B & og):A (og), k (og.k)
{
};
};
int
main ()
{
B b;
b.i = 99;
b.k = 22;
B n (b);
cout << n.i;
cout << n.k;
return 0;
}
This comment has been removed by a blog administrator.
ReplyDelete