There are now two classes, an abstracted parent and an inherited child.
That parent class will be exported from a head file to be a .dll file but child not be.
Assuming that we use class.cpp file to successfully generate a .dll file and
only use main.cpp file to generate a .exe file linked with this .dll file.
//class.h
#include <iostream>
using namespace std;
class __declspec(dllexport) parent
{
virtual ~parent() = 0;
virtual void print() = 0;
// get a child class object to add to a child object linked list.
// need a parent pointer to point to a child object
static parent* getChildObj();
// delete a child class object from a child object linked list
static void delObj(parent* obj);
};
class child : public parent
{
int otherData;
static child* head;
child* next;
public:
friend parent* parent::getChildObj();
friend void parent::delObj(parent* obj);
inline void print() override
{
cout << "child: hello world";
}
};
//class.cpp (generated a .dll file)
//this just is a roughly implementation process,
parent* getChildObj()
{
child* temp = new child;
......
return temp;
}
void delObj(parent* obj)
{
child* temp = child::head;
for(; temp; temp = temp->next)
{
......
delete temp;
}
}
//main.cpp (generated a main.exe program)
#pragma comment(lib, "___.lib")
class __declspec(dllimport) parent
{
virtual ~parent() = 0;
virtual void print() = 0;
static parent* getChildObj();
static void delObj(parent* obj);
};
int main()
{
//this step is normal and able to return a pointer.
parent* p = parent::getChildObj();
//the vtable pointer is nullptr, generating an exception.
p->print();
}
I want more details of how to export a class from a .dll file and how vtable pointer changed when we need to provide a library with only necessary information.
For this question,it is not necessary to have an answer. If you have a better method, thanks for your help.
skyWing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4