Skip to main content
added 7 characters in body
Source Link
ocomfd
  • 5.8k
  • 8
  • 33
  • 40

For example, suppose I have a class to create a button with specific styles common to my app:

class ButtonFactory{ public: static Button* createAppButton(std::string st,int font size){ Button* b=new Button(); //b->setColor();b->setText()...;b->playAnimation(...); return b; } } 

at other class

this->b=ButtonFactory::createAppButton("OK",20); . . . //delete it at destructor delete b; 

However, I feel uncomfortable that the 'new' and 'delete' are not at the same class. So my question is, should I also create a delete method in the ButtonFactory:

static void deleteAppButton(Button* b){ delete b; } . . . ButtonFactory::deleteAppButton(this->b); 

instead of delete b by keywords directly?

For example, suppose I have a class to create a button with specific styles common to my app:

class ButtonFactory{ public: static Button* createAppButton(std::string st,int font size){ Button* b=new Button(); //b->setColor();b->setText()...;b->playAnimation(...); return b; } } 

at other class

this->b=ButtonFactory::createAppButton("OK",20); . . . //delete it at destructor delete b; 

However, I feel uncomfortable that the 'new' and 'delete' are not at the same class. So my question is, should I also create a delete method in the ButtonFactory:

void deleteAppButton(Button* b){ delete b; } . . . ButtonFactory::deleteAppButton(this->b); 

instead of delete b by keywords directly?

For example, suppose I have a class to create a button with specific styles common to my app:

class ButtonFactory{ public: static Button* createAppButton(std::string st,int font size){ Button* b=new Button(); //b->setColor();b->setText()...;b->playAnimation(...); return b; } } 

at other class

this->b=ButtonFactory::createAppButton("OK",20); . . . //delete it at destructor delete b; 

However, I feel uncomfortable that the 'new' and 'delete' are not at the same class. So my question is, should I also create a delete method in the ButtonFactory:

static void deleteAppButton(Button* b){ delete b; } . . . ButtonFactory::deleteAppButton(this->b); 

instead of delete b by keywords directly?

Source Link
ocomfd
  • 5.8k
  • 8
  • 33
  • 40

Should a class which has a method to create object A also implement a method to delete A?

For example, suppose I have a class to create a button with specific styles common to my app:

class ButtonFactory{ public: static Button* createAppButton(std::string st,int font size){ Button* b=new Button(); //b->setColor();b->setText()...;b->playAnimation(...); return b; } } 

at other class

this->b=ButtonFactory::createAppButton("OK",20); . . . //delete it at destructor delete b; 

However, I feel uncomfortable that the 'new' and 'delete' are not at the same class. So my question is, should I also create a delete method in the ButtonFactory:

void deleteAppButton(Button* b){ delete b; } . . . ButtonFactory::deleteAppButton(this->b); 

instead of delete b by keywords directly?