How to make a common reusable function with different class types to reduce the code duplication using C++

john paul 41 Reputation points
2020-11-17T16:35:46.097+00:00

Hi All,

I am invoking the below function from different classes and files like as shown below.
And the implementation is almost similar. But it is initializing in the respective class constructor.

Can I create a common function for the below code for all the classes
and then when invoking, based on the class can I initialize and return the object?

Below is the function with almost similar implementation for all the classes. And I have this similar implementation in around 10 places.

Could someone please suggest me the better approach to create a common reusable function?

1.

 shared_ptr<CTestOneImpl> COneImpl::createTestObject(const string& f_strSFID, short f_nID,
                                                            bool f_bIsVerified,
                                                            bool f_bIsProcessed)
    {
    shared_ptr<CTestOneImpl> l_pTestObj = nullptr;
    l_pTestObj = make_shared<CTestOneImpl>(f_nID, f_strSFID,
                                                        f_bIsVerified, f_bIsProcessed,
                                                        this);
     return l_pTestObj;

}

2.

shared_ptr<CTestTwoImpl> CTwoImpl :: createTestObject(string f_hStrSFID, long f_nID,
                                                          bool f_bIsVerified,
                                                          bool f_bIsProcessed)
{
shared_ptr<CTestTwoImpl> l_pTestObj = nullptr;
l_pTestObj = make_shared<CTestTwoImpl>(f_nID, f_hStrSFID, f_bIsVerified
                                                , f_bIsProcessed, this);
return l_pTestObj;
}

3.

shared_ptr<CTestThreeImpl> CThreeImpl ::createTestObject(const string& f_strSFID,
                                                     const string& f_nID,
                                                     bool f_bIsVerified,
                                                     bool f_bIsProcessed)
{
shared_ptr<CTestThreeImpl> l_pTestObj = nullptr;
l_pTestObj = make_shared<CTestThreeImpl>(f_nID,
                                              f_strSFID,
                                              f_bIsVerified,
                                              f_bIsProcessed,
                                              this);
return l_pTestObj;
}
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,719 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. David Lowndes 4,716 Reputation points
    2020-11-17T16:41:42.887+00:00
    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.