How to pass the interface to the getinstnace of the class instead of passing concrete class in C++

Bhavya R 1 Reputation point
2020-08-19T12:41:21.767+00:00

Hello All,

In my test project, I am getting the instance of the signleton class (CPSCApp) and that instance I am passing it to the getinstance function of the second class (CCMainApp) .

To write the mock functions and set the expectations, I was asked to write an interface for CPSApp class.
So that I can pass this interface to the CCMainApp instead of passing the concrete CPSCApp class.

Below is the test project:

shared_ptr<CMainApp> m_hCMainAppObj;

bool CCMainAppTest::InitializeAppTest()
{
    UCPSCApp* pCPSCApp = CPSCApp::GetCPSCAppInstance();
    m_hCMainAppObj = CCMainApp::getInstance(pCPSCApp, 1);

    return m_hCMainAppObj != nullptr;
}

Invoking the below functionality from the test project:

PSCApp.h

static CPSCApp* GetCPSCAppInstance();

PSCApp.cpp

CPSCApp* CPSCApp::_pCPSCAppInstance = nullptr;

CPSCApp* CPSCApp::GetCPSCAppInstance()
{
    if(nullptr == _pCPSCAppInstance)
    {
        _pCPSCAppInstance = new CPSCApp() ;
    }
    return _pCPSCAppInstance ;
}

MainApp.h

static shared_ptr<CMainApp> getInstance(CPSCApp* f_pCPSCApp, int f_nStart);

static shared_ptr<CMainApp>         ms_hActiveInstance;

MainApp.cpp

shared_ptr<CMainApp> CMainApp :: ms_hActiveInstance = NULL;

shared_ptr<CMainApp> CMainApp::getInstance(CPSCApp* f_pCPSCApp, int f_nStart)
{
    if (NULL == ms_hActiveInstance)
    {
        ms_hActiveInstance = shared_ptr<CMainApp> (new CMainApp(f_pCPSCApp, f_nStart));
    }
    return  ms_hActiveInstance;
}

I created an interface IPSCApp from the CPSCApp class. But I am facing difficulty to pass this interface to the second class. Could someone please help me.

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,553 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Darran Rowe 561 Reputation points
    2020-08-19T13:02:41.84+00:00

    Are you trying to pass the shared_ptr around everywhere? If you are, don't.

    It is ok to obtain the pointer from out of the smart pointer.

    #include <memory>
    
    struct test
    {
    };
    
    void do_something(test *)
    {
        //do something to test
    }
    
    int main()
    {
        std::unique_ptr<test> t_ptr = std::make_unique<test>();
    
        do_something(t_ptr.get()); //obtains the pointer from the smart pointer
    
        return 0;
    }
    

    The smart pointer signifies what is responsible for deleting the block of memory. So unless you want to change this, it is recommended that you pass around the raw pointer.

    Also, if you want to create a singleton, there is a better option.

    struct test
    {
        static test *get_instance()
        {
            static test instance{};
    
            return &instance;
        }
    };
    
    int main()
    {
        test *instance = test::get_instance();
    
        return 0;
    }
    

    Because the local variable is static it has global lifetime, and this removes any need for memory management for this object too.

    0 comments No comments