dimanche 2 juillet 2017

Is it possible to call override function with different type of pointers in C++?

Primary class contains virtual functions=0. Next class inherit from primary class. Third class contains e.g. container of primary class and call overloaded functions. How to properly call override function with different type of pointers?

Code (something like visitor design pattern):

ClassDeclarations.h

class Computer;
class ComputerPart;
class ComputerPartDisplayVisitor;
class ComputerPartVisitor;
class Keyboard;
class Monitor;
class Mouse;

Computer.h

#ifndef COMPUTER_H
#define COMPUTER_H
#include <vector>
#include "ClassDeclarations.h"
#include "ComputerPart.h"
class Computer : public ComputerPart
{
    std::vector<std::shared_ptr<ComputerPart>> parts;
public:
    Computer() { parts.push_back(std::make_shared<Mouse>()); }
    void accept(const std::shared_ptr<ComputerPartVisitor> &computerPartVisitor) const override{
        for(const auto &part : parts) { part->accept(computerPartVisitor); }
        computerPartVisitor->visit(this);
    }
};
#endif  //COMPUTER_H

ComputerPart.h

#ifndef COMPUTER_PART_H
#define COMPUTER_PART_H
#include <memory>
#include "ClassDeclarations.h"
#include "ComputerPartVisitor.h"
class ComputerPart
{
public:
    virtual ~ComputerPart() {}
    virtual void accept(const std::shared_ptr<ComputerPartVisitor> &computerPartVisitor) const = 0;
};
#endif  //COMPUTER_PART_H

ComputerPartDisplayVisitor.h

#ifndef COMPUTER_PART_DISPLAY_VISITOR_H
#define COMPUTER_PART_DISPLAY_VISITOR_H
#include <iostream>
#include "ClassDeclarations.h"
#include "ComputerPartVisitor.h"
class ComputerPartDisplayVisitor : public ComputerPartVisitor
{
public:
    void visit(const std::shared_ptr<Computer> &computer) const override{std::cout << "ComputerPartDisplayVisitor visit() Computer" << std::endl;}    
    void visit(const std::shared_ptr<Mouse> &mouser) const override{std::cout << "ComputerPartDisplayVisitor visit() Mouse" << std::endl;}
    void visit(const std::shared_ptr<Keyboard> &keyboard) const override{std::cout << "ComputerPartDisplayVisitor visit() Keyboard" << std::endl;}    
    void visit(const std::shared_ptr<Monitor> &monitor) const override{std::cout << "ComputerPartDisplayVisitor visit() Monitor" << std::endl;}
};
#endif  //COMPUTER_PART_DISPLAY_VISITOR_H

ComputerPartVisitor.h

#ifndef COMPUTER_PART_VISITOR_H
#define COMPUTER_PART_VISITOR_H
#include <memory>
#include "ClassDeclarations.h"
#include "Computer.h"
#include "Mouse.h"
#include "Keyboard.h"
#include "Monitor.h"
class ComputerPartVisitor
{
public:
    virtual ~ComputerPartVisitor() {}
    virtual void visit(const std::shared_ptr<Computer> &computer) const = 0;
    virtual void visit(const std::shared_ptr<Mouse> &mouse) const = 0;
    virtual void visit(const std::shared_ptr<Keyboard> &keyboard) const = 0;
    virtual void visit(const std::shared_ptr<Monitor> &monitor) const = 0;
};
#endif  //COMPUTER_PART_VISITOR_H

Keyboard.h

#ifndef KEYBOARD_H
#define KEYBOARD_H
#include "ClassDeclarations.h"
#include "ComputerPart.h"
class Keyboard : public ComputerPart
{
public:
    void accept(const std::shared_ptr<ComputerPartVisitor> &computerPartVisitor) const override{computerPartVisitor->visit(this);}
};
#endif  //KEYBOARD_H

Monitor.h

#ifndef MONITOR_H
#define MONITOR_H
#include "ClassDeclarations.h"
#include "ComputerPart.h"
class Monitor : public ComputerPart
{
public:
    void accept(const std::shared_ptr<ComputerPartVisitor> &computerPartVisitor) const override{computerPartVisitor->visit(this);}
};
#endif  //MONITOR_H

Mouse.h

#ifndef MOUSE_H
#define MOUSE_H
#include "ClassDeclarations.h"
#include "ComputerPart.h"
class Mouse : public ComputerPart
{
public:
    void accept(const std::shared_ptr<ComputerPartVisitor> &computerPartVisitor) const override{computerPartVisitor->visit(this);}
};
#endif  //MOUSE_H

Main.cpp

#include "ClassDeclarations.h"
#include "Computer.h"
#include "ComputerPartDisplayVisitor.h"
int main(){
    Computer computer;
    computer.accept(std::make_shared<computerPartDisplayVisitor>());
}

Aucun commentaire:

Enregistrer un commentaire