C++ 父函数和子函数重载-如何访问父函数

C++ 父函数和子函数重载-如何访问父函数,c++,inheritance,overloading,C++,Inheritance,Overloading,以下是我想做的: class Msg { int target; public: Msg(int target): target(target) { } virtual ~Msg () { } virtual MsgType GetType()=0; }; inline std::ostream& operator <<(std::ostream& ss,Msg const& in) { return ss <&

以下是我想做的:

class Msg {
    int target;
public:
    Msg(int target): target(target) { }
    virtual ~Msg () { }
    virtual MsgType GetType()=0;
};

inline std::ostream& operator <<(std::ostream& ss,Msg const& in) {
    return ss << "Target " << in.target;
}

class Greeting : public Msg {
    std::string text;
public:
    Greeting(int target,std::string const& text) : Msg(target),text(text);
    MsgType GetType() { return TypeGreeting; }
};

inline std::ostream& operator <<(std::ostream& ss,Greeting const& in) {
    return ss << (Msg)in << " Text " << in.text;
}
class Msg{
int目标;
公众:
Msg(int-target):目标(target){}
虚拟~Msg(){}
虚拟MsgType GetType()=0;
};

内联std::ostream&operatorTry
ssit违反常量正确性。将
静态转换(in)
写入“in.text”而不是
(Msg)?您正在使用虚线符号访问私有成员..,这将导致编译错误。访问私有成员只能由getter和setter完成functions@ArunMu:或是朋友。或任何成员函数。标准并没有特别对待接受者和接受者。@David..是的..对。我错过了。
#include "iostream"
#include "string"

typedef enum {  TypeGreeting} MsgType;

class Msg {
    friend inline std::ostream& operator <<(std::ostream& ss,Msg const& in);

    int target;
public:
    Msg(int target): target(target) { }
    virtual ~Msg () { };
        virtual MsgType GetType()=0;
};

inline std::ostream& operator <<(std::ostream& ss,Msg const& in) {
    return ss << "Target " << in.target;
}

class Greeting : public Msg {
    friend inline std::ostream& operator <<(std::ostream& ss,Greeting const& in);

    std::string text;
public:
    Greeting(int target,std::string const& text) : Msg(target),text(text) {};
    MsgType GetType() { return TypeGreeting; }

};

inline std::ostream& operator <<(std::ostream& ss,Greeting const& in) {
    return ss << (Msg const&)in << " Text " << in.text;
}

int _tmain(int argc, _TCHAR* argv[])
{
    Greeting grt(1,"HELLZ");
    std::cout << grt << std::endl;
    return 0;
}