D
Dwight Army of Champions
When designing a class, how do you know whether to make a variable a
private data member or just an argument to the class's member
functions? If I make it a private data member, then it's imperative I
have a valuie for it at the time I create the object (put it in the
constructor). On the other hand, if I just use it as a member function
argument, then I have to be responsible for defining and initializing
the variable before the object is created, and then deleting the
variable after the object is deleted. THis leads to code like this:
// code begins here
// ... define and initialize many variables called MyVariable
MyClass* MyObject = new MyClass(this);
MyObject->DoSomething(MyVariable1, MyVariable2, /* ... */ );
MyObject->DoSomethingElse(MyVariable2, MyVariable6);
delete MyObject; MyObject = 0;
// delete many MyVariables
// code ends here
What is the best way to do this?
private data member or just an argument to the class's member
functions? If I make it a private data member, then it's imperative I
have a valuie for it at the time I create the object (put it in the
constructor). On the other hand, if I just use it as a member function
argument, then I have to be responsible for defining and initializing
the variable before the object is created, and then deleting the
variable after the object is deleted. THis leads to code like this:
// code begins here
// ... define and initialize many variables called MyVariable
MyClass* MyObject = new MyClass(this);
MyObject->DoSomething(MyVariable1, MyVariable2, /* ... */ );
MyObject->DoSomethingElse(MyVariable2, MyVariable6);
delete MyObject; MyObject = 0;
// delete many MyVariables
// code ends here
What is the best way to do this?