TUHIN SUBHRA PANDA
TUHIN SUBHRA PANDA

Reputation: 23

create object of abstract class c++

in my project I have IAcquirer.h file and the content of the file is like below

class IAcquirer {

        public:
            IAcquirer() {}
            virtual ~IAcquirer() {}
            virtual int initAcquirer(std::string ) = 0;
            virtual int processTransactionToHost(const transaction::Transaction &tr, transaction::TransactionDataContainer &txnDataContainer) =0;
            virtual int connect(bool) =0;
            virtual int disconnect(void) =0;
        };

Now in another file called system.cpp , object of the class IAcquirer is created

boomer::host::IAcquirer *pAcquirer;
pAcquirer->processTransactionToHost(****,***);

But from my understanding we cant create object of abstract class , Now the object is being created in other file

Upvotes: 1

Views: 6513

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727077

Your understanding is correct, you cannot create an object of an abstract class.

However, you can create objects derived from an abstract class. These objects are compatible with their base abstract class, and could be manipulated through a pointer or a reference typed as the abstract base.

For example, this is valid:

class RealAcquirer : public IAcquirer {
    virtual int initAcquirer(std::string ) {
        cout << "hello" << endl;
        return 0;
    }
    ... // Other pure virtual member functions implememted here
};

IAcquirer *make() {
    return new RealAcquirer();
}

Once you get a pointer to IAcquirer, you can call any of IAcquirer's member functions through it, including pure virtual member functions overridden in the derived class.

Upvotes: 4

Related Questions