2

I want to create an array of objects and I want to use a certain constructor.

std::vector<Modul> arrDigOut; 
arrDigOut.push_back(Modul(IDC_CHECK1, this, "GVL.DigOut1", pAddr));

This works as long as DigOut is not a derived class. When I derive it and use the class DigOut it fails:

class Modul
{
protected:
    int         id;
    int         nErr;
    void*       plcVar;
    bool        bDigOut;
    PAmsAddr    pAddr;
    ULONG       lHdlVar;
    CButton*    pBt;

public:
    Modul();

    //Modul(int ID, Cbeckhoff_frontendDlg* pCbeckhoff,void* pVar,PAmsAddr pAdr)
    //{
    //    SetID(ID,pCbeckhoff);
    //    plcVar = pVar;
    //    pAddr = pAdr;
    //};

    int         GetID() { return (id); };
    void*       GetplcVar() { return plcVar; };
    void        SetID(int ID, Cbeckhoff_frontendDlg* pCbeckhoff);
    int         InitCheck(Cbeckhoff_frontendDlg* pCbeckhoff);
    CButton*    GetpBt(){return pBt;};
    void        SetButton( Cbeckhoff_frontendDlg* pCbeckhoff);
};

class DigOut : protected Modul
{
public:
    DigOut();
    DigOut(int ID, Cbeckhoff_frontendDlg* pCbeckhoff,void* pVar,PAmsAddr pAdr)
    {
        SetID(ID,pCbeckhoff);
        plcVar = pVar;
        pAddr = pAdr;
    };
};
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
womdom
  • 55
  • 1
  • 6

1 Answers1

9

That's because of object slicing. You'll need to have a vector of pointers or smart pointers to the base class:

std::vector<Modul*> arrDigOut; 
arrDigOut.push_back(new Modul(IDC_CHECK1, this,"GVL.DigOut1",pAddr));
arrDigOut.push_back(new DigOut());

Remember to free the memory if not using smart pointers.

When you have a std::vector<Modul> arrDigOut; and you insert on object of a derived type, it will be sliced and becomes a Modul. The Modul part of the object is copied in the vector, and the rest is lost.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Searching for "object slicing" brought me further in understanding, but still your example doesn't work in my code, neither with new Module nor with new DigOut. Is there a problem with the "this" operator? – womdom Jul 18 '12 at 12:51
  • sorry,first time I use stackoverflow, didn't know it sends the message directly when tapping – womdom Jul 18 '12 at 12:56
  • error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall DigOut::DigOut(void)" (??0DigOut@@QAE@XZ)" in Funktion ""protected: virtual int __thiscall Cbeckhoff_frontendDlg::OnInitDialog(void)" (?OnInitDialog@Cbeckhoff_frontendDlg@@MAEHXZ)". It complains that it cannot resolve reference to an external symbol. – womdom Jul 18 '12 at 13:04
  • 1
    You didn't implement the default constructor. Replace `DigOut();` with `DigOut() {};`. – Luchian Grigore Jul 18 '12 at 13:04
  • @user1534683 sure, glad to help. In the future, also post the error message for a speedier resolution. – Luchian Grigore Jul 18 '12 at 13:13
  • NOTE: You can also store reference_wrappers of the base type if you don't want to deal with pointers. References in C++ are also polymorphic. – bstamour Jul 18 '12 at 16:41