how to copy function contents to another

Aug 13, 2012 at 6:03pm
Hello,

I'm wondering if it's possible to use one function's contents to another. This would be handy when overwriting a function in a derived class and only small changes are needed. eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class A
{
public:
  void something (param1 ) { eg. vector <int>... };
}

class B:public A
{
public:
   int something (param2) 
   {
     ... exactly the same thing as "A::something()" but just a few extra lines
    }
}


I found about constructors initialization list, such as B(arguments):A(arguments), but couldn't make it work in my code.

Thanks!
Aug 13, 2012 at 6:23pm
Sure, just call the first function from the 2nd.
Aug 15, 2012 at 8:02am
Sorry for the confusion, I ment something like this (note that only the last two lines of the second constructor are all the modifications I need):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
UIAnswerPanel::UIAnswerPanel(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(UIAnswerPanel)
	Create(parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("id"));
	mainSizer = new wxBoxSizer(wxHORIZONTAL);
	mCheckBox = new wxCheckBox(this, ID_CHECKBOX1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
	mCheckBox->SetValue(false);
	mainSizer->Add(mCheckBox, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	mTextCtrl = new wxTextCtrl(this, ID_TEXTCTRL1, _("AnswerTextCtrl"), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxVSCROLL, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	mTextCtrl->SetMaxLength(200);
	mainSizer->Add(mTextCtrl, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
	SetSizer(mainSizer);
	mainSizer->Fit(this);
	mainSizer->SetSizeHints(this);
	//*)
	mTextCtrl->Connect(wxEVT_SET_FOCUS,(wxObjectEventFunction)&UIAnswerPanel::OnSetFocus, NULL, this);
	this-> SetName (_("answerPanel"));

}

UIAnswerPanel::UIAnswerPanel(wxWindow* parent, Answer answerArg, wxWindowID id,const wxPoint& pos,const wxSize& size)
{//
	//
	// Can't use wxSmith to edit the constructor's arguments, just copy paste the constructor after any modifications.
	// ........
	//
		Create(parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("id"));
	mainSizer = new wxBoxSizer(wxHORIZONTAL);
	mCheckBox = new wxCheckBox(this, ID_CHECKBOX1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
	mCheckBox->SetValue(false);
	mainSizer->Add(mCheckBox, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	mTextCtrl = new wxTextCtrl(this, ID_TEXTCTRL1, _("AnswerTextCtrl"), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxVSCROLL, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	mTextCtrl->SetMaxLength(200);
	mainSizer->Add(mTextCtrl, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
	SetSizer(mainSizer);
	mainSizer->Fit(this);
	mainSizer->SetSizeHints(this);
	//*)
	mTextCtrl->Connect(wxEVT_SET_FOCUS,(wxObjectEventFunction)&UIAnswerPanel::OnSetFocus, NULL, this);
	this-> SetName (_("answerPanel"));
	///////////// .....

	mTextCtrl -> SetValue (answerArg . GetText() );
	mCheckBox -> SetValue (answerArg . GetRight());
}
Aug 15, 2012 at 10:14am
This is a very common issue - many classes may have a lot of initialization to do, and having to repeat this initialisation in every construtor is prone to errors.

Just make some (private) intitialisation function and call that from the constructor

For example purposes only:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MyClass
{
	public:
	MyClass (....)  /some constructor
	{
		ObjectInit (...) ;
		//Maybe other stuff relevant to this constructor
	}
	
	MyClass  (...) //another constructor
	{
		
		ObjectInit(...);
		
		//Maybe other stuff relevant to this constructor
	}
	
	private:
	void ObjectInit (....)
	{
	
		//Common initialization
	}
	
};
	

Last edited on Aug 15, 2012 at 10:17am
Aug 16, 2012 at 10:07am
Thanks guestgulkan, I think your example is the best way to do it. (most reliable)
Topic archived. No new replies allowed.