Click here to Skip to main content
16,005,389 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: user defined events? Pin
mangellj6-Apr-04 3:56
mangellj6-Apr-04 3:56 
GeneralC++ help - getting object types Pin
Anonymous6-Apr-04 2:17
Anonymous6-Apr-04 2:17 
GeneralRe: C++ help - getting object types Pin
Cedric Moonen6-Apr-04 2:29
Cedric Moonen6-Apr-04 2:29 
GeneralRe: C++ help - getting object types Pin
thowra6-Apr-04 2:58
thowra6-Apr-04 2:58 
GeneralRe: C++ help - getting object types Pin
Cedric Moonen6-Apr-04 3:09
Cedric Moonen6-Apr-04 3:09 
GeneralRe: C++ help - getting object types Pin
thowra6-Apr-04 6:18
thowra6-Apr-04 6:18 
GeneralRe: C++ help - getting object types Pin
Maximilien6-Apr-04 2:59
Maximilien6-Apr-04 2:59 
GeneralRe: C++ help - getting object types Pin
Andrew Walker6-Apr-04 4:49
Andrew Walker6-Apr-04 4:49 
It depends on how you think. Some people like RTTI, some people like home grown (enums and typecodes), other people don't use it at all. To prevent casting, and to get the cleanest possible OO design use the 'Visitor' pattern from the gang of four book.

The general idea is you use double-dispatch to sort out the type information

#include <iostream>
#include <vector>

using namespace std;

class Visitor;

class CEmployee 
{
public:
    virtual void accept(Visitor& v);

    virtual void foo() 
    { 
        cout << "employee" << endl; 
    }
};

class CSalesman : public CEmployee
{
public:
    virtual void accept(Visitor& v);

    virtual void getCommision() 
    {  
        cout << "commision" << endl;
    }

    virtual void foo()
    {
        cout << "salesman" << endl;
    }
};

class CStaff : public CEmployee
{
public:
    virtual void accept(Visitor& v);

    virtual void foo()
    {
        cout << "staff" << endl;
    }
};

class Visitor
{
public:
    virtual void visit(CEmployee* pA) = 0;
    virtual void visit(CSalesman* pB) = 0;
    virtual void visit(CStaff* pC) = 0;
};

void CEmployee::accept(Visitor& v)
{
    v.visit(this);
}

void CStaff::accept(Visitor& v)
{
    v.visit(this);
}

void CSalesman::accept(Visitor& v)
{
    v.visit(this);
}

class CommisionVis : public Visitor
{
public:
    virtual void visit(CEmployee* pEmp)
    {
        // do nothing
    }

    virtual void visit(CSalesman* pEmp)
    {
        pEmp->getCommision();
    }

    virtual void visit(CStaff* pEmp)
    {
        // do nothing
    }
};


int main()
{
    std::vector<CEmployee*> empList;
    empList.push_back(new CEmployee());
    empList.push_back(new CSalesman());
    empList.push_back(new CStaff());
    for(size_t i = 0; i < empList.size(); i++)
    {
        empList.at(i)->foo();
    }
    cout << "**************************" << endl;
    CommisionVis vis;
    for(size_t i = 0; i < empList.size(); i++)
    {
        empList.at(i)->accept(vis);
    }
    // ... more code
}



If you can keep you head when all about you
Are losing theirs and blaming it on you;
If you can dream - and not make dreams your master;
If you can think - and not make thoughts you aim;
Yours is the Earth and everything that's in it.

Rudyard Kipling

GeneralRe: C++ help - getting object types Pin
Nemanja Trifunovic6-Apr-04 5:05
Nemanja Trifunovic6-Apr-04 5:05 
GeneralRe: C++ help - getting object types Pin
Maxwell Chen6-Apr-04 16:45
Maxwell Chen6-Apr-04 16:45 
GeneralRe: C++ help - getting object types Pin
David Crow6-Apr-04 4:57
David Crow6-Apr-04 4:57 
GeneralRe: C++ help - getting object types Pin
thowra6-Apr-04 6:16
thowra6-Apr-04 6:16 
GeneralDebug service Pin
Anonymous6-Apr-04 0:56
Anonymous6-Apr-04 0:56 
GeneralRe: Debug service Pin
Milton Karimbekallil6-Apr-04 1:15
Milton Karimbekallil6-Apr-04 1:15 
Generalstock quote Pin
Anonymous6-Apr-04 0:54
Anonymous6-Apr-04 0:54 
Generalbitmap Pin
m_vnaresh6-Apr-04 0:46
m_vnaresh6-Apr-04 0:46 
GeneralRe: bitmap Pin
avenger_sb256-Apr-04 7:36
avenger_sb256-Apr-04 7:36 
GeneralRe: bitmap Pin
John R. Shaw6-Apr-04 16:48
John R. Shaw6-Apr-04 16:48 
GeneralMenu Pin
Rassul Yunussov6-Apr-04 0:07
Rassul Yunussov6-Apr-04 0:07 
GeneralRe: Menu Pin
Empty19816-Apr-04 0:36
Empty19816-Apr-04 0:36 
GeneralRe: Menu Pin
Antony M Kancidrowski6-Apr-04 1:22
Antony M Kancidrowski6-Apr-04 1:22 
GeneralRe: Menu Pin
Rassul Yunussov6-Apr-04 22:30
Rassul Yunussov6-Apr-04 22:30 
GeneralRe: Menu Pin
Antony M Kancidrowski7-Apr-04 2:12
Antony M Kancidrowski7-Apr-04 2:12 
Generalavenger_sb25//read, write data files Pin
dairiseky5-Apr-04 23:53
dairiseky5-Apr-04 23:53 
GeneralRe: avenger_sb25//read, write data files Pin
Steve S6-Apr-04 1:21
Steve S6-Apr-04 1:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.