Question

I have following class hierarchy representing DNS:

class CZone {
    string                  m_Name;
    vector<RecordSurrogate> m_Records;
public:
                        CZone       ( string name ) : m_Name(name) {};
    bool                Add         ( const CRecord & );
    bool                Del         ( CRecord & );
    vector<CRecord>     Search      ( const string & ) const;
    void                Print       ( ostream & os ) const;
    friend ostream &    operator<<  ( ostream & os, const CZone & );
};

With Add() and Del() methods I would like to check whether there is record with given name in m_Records already present. I can’t use typeid here, however I can recognize different records with value returned by Type().

I wrote bool CZone::recEqual(const CRecord &lhs, const CRecord &rhs) const medhod but got stuck with casting complaints like

No matching conversion for static_cast from 'const CRecord' to 'const CRecA'

for this construct:

bool CRecA::isEqual( const CRecord & rhs ) const {
    return ( m_Name == rhs.m_Name && m_IP == static_cast<const CRecA>(rhs) . m_IP );
}

Base class:

class CRecord {
protected:
    string      m_Name;
public:
                            CRecord     ( void ) = default;
                            CRecord     ( string & name ) : m_Name(name) {}
                string      Name        ( void ) const { return m_Name; }
    virtual     string      Type        ( void ) const = 0;
    virtual    ~CRecord                 ( void ) {};
};

Derived classes:

class CRecA : public CRecord {
    CIPv4       m_IP;
public:
                CRecA       ( void ) = default;
    string      Type        ( void ) const { return "A"; }
    ostream &   operator << ( ostream & os ) const;
    CRecord *   copy        ( void ) const;

};

class CRecMX : public CRecord {
    string      m_Server;
    int         m_Prio;
public:
                CRecMX      ( void ) : m_Prio(0) {};
                CRecMX      ( string name, string server, int prio ) :
                    CRecord::CRecord(name), m_Server(server), m_Prio(prio) {}
    string      Type        ( void ) const { return "MX"; }
    ostream &   operator << ( ostream & os ) const;
    CRecord *   copy        ( void ) const;
};

Surrogate class:

class RecordSurrogate {
public:
                        RecordSurrogate ( void );
                        RecordSurrogate ( const CRecord & );
                       ~RecordSurrogate ( void );
                        RecordSurrogate ( const RecordSurrogate & );
    RecordSurrogate &   operator=       ( const RecordSurrogate & );
    string              Name            ( void ) const;
    string              Type            ( void ) const;
private:
    CRecord * rp;
};
Était-ce utile?

La solution

The additional code in the comments shows a mistake:

bool CRecA::isEqual(const CRecord& rhs) const
{
    return m_Name == rhs.m_Name && m_IP == static_cast<const CRecA>(rhs).m_IP;
}

You try to cast a CRecord const&to a CRecA const, which is incorrect. You need to cast it to CRecA const& instead.

The reason for this is quite simple: You wish to interpret the original object as its derived class - and not convert a CRecord object to a CRecA object.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top