Hello Guys!!!
How to dynamically allocate memory in constructors or setter functions for data members of class? Suppose if we have two data members(ID,Name) and two setter functions (void setID(),void setName()) then how to allocate memory dynamically for these two data members in these setter functions or constructors....Please if anybody knows reply here in detail and help others....Thanks!!!
The obvious place to do that is in the constructor. It would be far better not to allocate the memory dynamically, though. Is there a reason why something like this would not work?
1 2 3 4
class someKindOfObject{
int ID;
string Name;
}
If ID and Name are just simple variables, there is no reason to allocate memory dynamically.
No, these are not just simple variables....there is also one DeleteMember() function that is used to deallocate memory for these variables that's why i am asking you guys. DeleteMember() function only works if we allocate memory dynamically for these two variables and i am totally confused where to allocate memory for these variables (In setters or in constructors).....Confused...... :(
Those variables are not at all complex. They are very simple and you've chosen to make them complicated. C++ provides many options so that you don't have to do this sort of thing.
If you are forced to do it this way, you have my sympathy.
That said, you've got it wrong. Is ID meant to be one number, or ten numbers?
Whether to dynamically allocate memory or not should be left up to the user of the class (or given a facility to choose how the memory is handle i.e. stl containers).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
struct Bob
{
int dude;
int wheres;
int my;
int car;
};
int main()
{
Bob* lol = new Bob; //Those variables are now dynamically allocated.
Bob test; //However, stack is faster and this is more effecient to allocate on stack.
Bob* wtf = new Bob[150]; //But in an array, Bob can start getting fat and is too big for stack. Stack is limited.
std::vector<Bob> hmmm; //Vectors do not allocate on stack (? true ?)
}
Setter functions don't reallocate. They simply change the value of you data. Constructors handle the setting of the creation of variables, not how they're allocated.
That's what I'm asking,. You made ID an array of ten int values, and then you tried to treat it as if it were a single int value. If it's just one int value, why did you write this:
int *ID=newint[10];
That's an array of ten int values.
but i already told you i have DeleteMember() function also where i want to delete/deallocate memory for these variables.... that's why dynamic memory allocation is necessary.....
The idea of a DeleteMember() function is bad both in practice and in concept. I hope this isn't a homework assignment... I had to deal with stuff like that recently as it was being submitted to me as "valid" when it was nowhere close.
but i already told you i have DeleteMember() function also where i want to delete/deallocate memory for these variables.... that's why dynamic memory allocation is necessary.....
Are you being forced to do that? Because it is silly and unnecessary. It's extra work that is not needed. There is no need to allocate dynamically for this. No need. If you do, you are making it harder for yourself.
I want to implement my DeleteMember() function in derived class (Members) where i want to delete record of Users(ID,Name)......The DeleteMember() function that i want to implement in Members class is like this:
You do know that the delete keyword won't actually delete the member object? The pointers will still exist and they will still point to the same place. The member object will still exist. The data will not be magically removed.
Here is the constructor you need for what you want to do; I point out again that what you want to do is silly and makes no sense at all and is making this much harder for yourself than it needs to be.
1 2 3 4 5 6 7 8 9 10
User(){
ID = newint;
Name = newchar[25];
}
void DeleteMember()
{
delete ID;
delete[] Name;
}