Object Creation of an class?
-
Hi, I am developing an app for mobile using C++, I am calling DownloadManager from Homectrl.CPP, Then came to my mind, which will one will efficient and object will be nullified. What is the exact difference between DownloadManager objDownloadManager; and DownloadManager *objDownloadManager; Which one will be used? Some good insight will be appreciated.
-
Hi, I am developing an app for mobile using C++, I am calling DownloadManager from Homectrl.CPP, Then came to my mind, which will one will efficient and object will be nullified. What is the exact difference between DownloadManager objDownloadManager; and DownloadManager *objDownloadManager; Which one will be used? Some good insight will be appreciated.
It makes no difference whether you are dealing with an object or a standard item like an int, float or double. This form you have created the item type ... these all do the same
int i; // You created an integer type called i
double d; // You created a double called d
DownloadManager objDownloadManager; // You created a DownloadManager called objDownloadManagerThis form you have created a POINTER TO THE ITEM ... it's most likely just 4 bytes (assuming 32bit compilation). As we haven't set them to anything they are effectively undefined and could be pointing anywhere.
int* i; // You created a pointer to an integer type called i
double* d; // You created a pointer to a double type called d
DownloadManager* objDownloadManager; // You created a pointer to a DownloadManager type called objDownloadManagerSo they are vastly different and not something you should ever be confused about .. NOT EVER. So if you like the second form is like the address to your house, the first form is your ACTUAL PHYSICAL house. What you are doing is telling me that if I write text for the address of a house I can live in the text because it's the same thing as a physical house and it's confusing to you. You use the pointer form for the same reason you use a written address for you house. Its a bit tiresome and hard to carry the whole house around just so people can send you mail. The alternative reason might be because your house hasn't yet been built to carry around :-)
In vino veritas
-
Hi, I am developing an app for mobile using C++, I am calling DownloadManager from Homectrl.CPP, Then came to my mind, which will one will efficient and object will be nullified. What is the exact difference between DownloadManager objDownloadManager; and DownloadManager *objDownloadManager; Which one will be used? Some good insight will be appreciated.
Hi, Some practical thinking: Case 1. You declare 'DownloadManager' as a member of another class: The object declaration (DownloadManager objDownloadManager) means, objDownloadManager has the same life time of your parent (the class in which you have declared it) class. But, pointer member means objDownloadManager will live even if his parent die. Case 2. You declare 'DownloadManager' in a function: The object gets destructed just after function scope. But the pointer declaration, can live until you delete it either inside or outside the function scope (provided you keep the address). Thats all I know.
-
Hi, I am developing an app for mobile using C++, I am calling DownloadManager from Homectrl.CPP, Then came to my mind, which will one will efficient and object will be nullified. What is the exact difference between DownloadManager objDownloadManager; and DownloadManager *objDownloadManager; Which one will be used? Some good insight will be appreciated.
One other point to remember - if you declare a pointer to the object (the second declaration), you need to explicitly call the constructor and destructor. When declaring it as an instance (first declaration), the constructor is called automatically at the point of the declaration, and the destructor is called when it goes out of scope.
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
-
One other point to remember - if you declare a pointer to the object (the second declaration), you need to explicitly call the constructor and destructor. When declaring it as an instance (first declaration), the constructor is called automatically at the point of the declaration, and the destructor is called when it goes out of scope.
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
Thanks Mick
-
Hi, Some practical thinking: Case 1. You declare 'DownloadManager' as a member of another class: The object declaration (DownloadManager objDownloadManager) means, objDownloadManager has the same life time of your parent (the class in which you have declared it) class. But, pointer member means objDownloadManager will live even if his parent die. Case 2. You declare 'DownloadManager' in a function: The object gets destructed just after function scope. But the pointer declaration, can live until you delete it either inside or outside the function scope (provided you keep the address). Thats all I know.
Thanks Krishna Kumar
-
It makes no difference whether you are dealing with an object or a standard item like an int, float or double. This form you have created the item type ... these all do the same
int i; // You created an integer type called i
double d; // You created a double called d
DownloadManager objDownloadManager; // You created a DownloadManager called objDownloadManagerThis form you have created a POINTER TO THE ITEM ... it's most likely just 4 bytes (assuming 32bit compilation). As we haven't set them to anything they are effectively undefined and could be pointing anywhere.
int* i; // You created a pointer to an integer type called i
double* d; // You created a pointer to a double type called d
DownloadManager* objDownloadManager; // You created a pointer to a DownloadManager type called objDownloadManagerSo they are vastly different and not something you should ever be confused about .. NOT EVER. So if you like the second form is like the address to your house, the first form is your ACTUAL PHYSICAL house. What you are doing is telling me that if I write text for the address of a house I can live in the text because it's the same thing as a physical house and it's confusing to you. You use the pointer form for the same reason you use a written address for you house. Its a bit tiresome and hard to carry the whole house around just so people can send you mail. The alternative reason might be because your house hasn't yet been built to carry around :-)
In vino veritas
Thanks In vino veritas.