Skip to content

Instantly share code, notes, and snippets.

@Bueddl
Last active February 24, 2017 23:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bueddl/9f517d92413c17b71a57c6334a830051 to your computer and use it in GitHub Desktop.
Save Bueddl/9f517d92413c17b71a57c6334a830051 to your computer and use it in GitHub Desktop.
#pragma once
#include <iostream>
class IPlugin
{
public:
virtual void doSth() = 0; // muss implementiert werden
virtual void otherToDo()
{
std::cout << "default impl called" << std::endl;
}
};
#include "IPlugin.h" // eig nicht nötig
#include "MyPlugin.h"
#include "MyOtherPlugin.h"
#include <vector>
#include <memory> // smart pointer
int main()
{
std::vector<std::shared_ptr<IPlugin>> plugins;
plugins.emplace_back(std::make_shared<MyPlugin>());
plugins.emplace_back(std::make_shared<MyOtherPlugin>());
for (auto *plugin : plugins) {
plugin->doSth();
plugin->otherToDo();
}
// jetzt kümmert sich vector destructor um das aufräumen der shared_ptr und der shared_ptr kümmert sich ums aufräumen der Plugin Objekte
}
#pragma once
#include "IPlugin.h"
#include <iostream>
class MyOtherPlugin : public IPlugin
{
public:
void doSth() override
{
std::cout << "work work work" << std::endl;
}
};
#pragma once
#include "IPlugin.h"
#include <iostream>
class MyPlugin : public IPlugin
{
public:
void doSth() override
{
std::cout << "i should so something..." << std::endl;
}
void otherToDo() override
{
std::cout << "I am special" << std::cout;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment