Inheritance class design with inappropriate methods

Say I have the following abstract base class:

class DLAContainer {
public:
    DLAContainer() { std::random_device rd; mt_eng = std::mt19937(rd()); }
    virtual void generate(std::size_t _n) = 0;
protected:
    std::mt19937 mt_eng;

    virtual void spawn_particle(int& _x, int& _y,
        std::uniform_real_distribution<>& _dist) = 0;
    virtual void spawn_particle(int& _x, int& _y, int& _z,
        std::uniform_real_distribution<>& _dist) = 0;

    // ... among other methods to be overridden...
};

and two classes that inherit from DLAContainer:

class DLA_2d : public DLAContainer {
public:
    DLA_2d() : DLAContainer() { // initialise stuff }
    void generate(std::size_t _n) { // do stuff }
private:;
    std::queue<std::pair<int,int>> batch_queue;
    // ...

    void spawn_particle(int& _x, int& _y, 
        std::uniform_real_distribution<>& _dist) { // do stuff }
    void spawn_particle(int& _x, int& _y, int& _z,
        std::uniform_real_distribution<>& _dist) { // do nothing }

    //...
};

and

class DLA_3d : public DLAContainer {
public:
    DLA_3d() : DLAContainer() { // initialise stuff }
    void generate(std::size_t _n) { // do stuff }
private:;
    std::queue<std::tuple<int,int,int>> batch_queue;
    // ...

    void spawn_particle(int& _x, int& _y, 
        std::uniform_real_distribution<>& _dist) { // do nothing }
    void spawn_particle(int& _x, int& _y, int& _z,
        std::uniform_real_distribution<>& _dist) { // do stuff }

    //...
};

As you can see, there are two overloads spawn_particle- one for the 2D lattice and the other for 3D, however both are pure virtualfunctions and therefore must be redefined / implemented both in DLA_2dand DLA_3dwhere the 3D method will do nothing in DLA_2dand vice versa for DLA_3d.

Of course, this works, and everything works fine, but I can't help but feel that the design is a bit awkward when you have to apply irrelevant methods in each class.

, , (.. ISpawnParticle_2d ISpawnParticle_3d) ? , ?

: , DLAContainer ( ). ( DLA_2d, DLA_3d), , spawn_particle - DLAContainer .

+4
2

, . OO-: , IS A .

:

DLA_3d d3;
d3.spawn_particle(...) //The 2D version
//and
DLA_2d d2;
d2.spawn_particle(...) //The 3D version

-, " ", . , , spawn_particle, , :

  • .
  • , , .

/ . .

PS: , . : " ..."


. , , .
:

  • , : .
    • : / , / .
    • , DLAContainer . ?
  • , ? , . ? ( ).
  • spawn_particle . ( , .)
  • "". " " , "" I.e. 2D- .
+2

, . , 2D 3D-? , , , , , : .

, , , ? virtual, , , .

struct DLAContainerBase
{
  /* some basic virtual interface */
};

template<int Dims>
struct DLAContainer : DLAContainerBase
{
  virtual void spawn_particle(std::array<int,Dims>&,
                              std::uniform_real_distribution<>&) = 0;
};

, , . Btw, (, , ).

+2

All Articles