C++ 访问派生类成员的基类函数

C++ 访问派生类成员的基类函数,c++,class,inheritance,C++,Class,Inheritance,下面是我试图做的一个例子。基本上,我的GetInt()函数是在基类A中定义的,我的派生类B也应该使用相同的定义。但问题是,当我从类B的对象访问GetInt()时,我希望返回派生类的成员,而不是基类的成员。但以下代码的输出给出: Constructor A called Constructor A called Constructor B called Object A: 10 Object B: 10 鉴于我希望它是: Constructor A called Constructor A ca

下面是我试图做的一个例子。基本上,我的
GetInt()
函数是在基类
A
中定义的,我的派生类
B
也应该使用相同的定义。但问题是,当我从类
B
的对象访问
GetInt()
时,我希望返回派生类的成员,而不是基类的成员。但以下代码的输出给出:

Constructor A called
Constructor A called
Constructor B called
Object A: 10
Object B: 10
鉴于我希望它是:

Constructor A called
Constructor A called
Constructor B called
Object A: 10
Object B: 5
我的代码:

class A{
        public:
                A(){
                        std::cout << "Constructor A called" << std::endl;
                        m_nA = 10;
                }
                int GetInt(){
                        return m_nA;
                }
        private:
                int m_nA;
};
class B : public A{
        public:
                B(){
                        std::cout << "Constructor B called" << std::endl;
                        m_nA = 5;
                }
        private:
                int m_nA;
};

int main(){
        A ObjA; B ObjB;
        std::cout << "Object A: " << ObjA.A::GetInt() << std::endl;
        std::cout << "Object B: " << ObjB.B::GetInt() << std::endl;
        return 0;
}
A类{
公众:
(){

std::cout您需要将
GetInt()设为虚拟,并在子类中重写它:

class A{
    public:
        A(){
            std::cout << "Constructor A called" << std::endl;
            m_nA = 10;
        }

        virtual int GetInt(){
            return m_nA;
        }

    private:
        int m_nA;
};

class B : public A{
    public:
        B(){
            std::cout << "Constructor B called" << std::endl;
            m_nA = 5;
        }

        virtual int GetInt(){
            return m_nA;
        }

    private:
        int m_nA;
};
由于您是通过指定类型
A:
B:
显式调用该方法的,因此不一定需要将其设置为虚拟的-但是您将无法通过指向
A
的指针或引用来调用
B::GetInt()
(无后期绑定):

使用非虚拟方法:

Object B through A&: 5
Object B through A&: 10

您需要将
GetInt()
设为虚拟,并在子类中重写它:

class A{
    public:
        A(){
            std::cout << "Constructor A called" << std::endl;
            m_nA = 10;
        }

        virtual int GetInt(){
            return m_nA;
        }

    private:
        int m_nA;
};

class B : public A{
    public:
        B(){
            std::cout << "Constructor B called" << std::endl;
            m_nA = 5;
        }

        virtual int GetInt(){
            return m_nA;
        }

    private:
        int m_nA;
};
由于您是通过指定类型
A:
B:
显式调用该方法的,因此不一定需要将其设置为虚拟的-但是您将无法通过指向
A
的指针或引用来调用
B::GetInt()
(无后期绑定):

使用非虚拟方法:

Object B through A&: 5
Object B through A&: 10

您正在调用
A::GetInt()
,它将始终返回
A::m_nA

除非您可以更改
B::m_nA
的名称,否则您只能使用虚拟函数:

class A{
        public:
                A() : m_nA(10){
                        std::cout << "Constructor A called" << std::endl;
                }
                virtual int GetInt(){
                        return m_nA;
                }
        private:
                int m_nA;
};
class B : public A{
        public:
                B() : A(), m_nA(5){
                        std::cout << "Constructor B called" << std::endl;
                }
                virtual int GetInt(){
                        return m_nA;
                }
        private:
                int m_nA;
};
A类{
公众:
A():m_nA(10){

std::cout您正在调用
A::GetInt()
,它将始终返回
A::m_nA

除非您可以更改
B::m_nA
的名称,否则您只能使用虚拟函数:

class A{
        public:
                A() : m_nA(10){
                        std::cout << "Constructor A called" << std::endl;
                }
                virtual int GetInt(){
                        return m_nA;
                }
        private:
                int m_nA;
};
class B : public A{
        public:
                B() : A(), m_nA(5){
                        std::cout << "Constructor B called" << std::endl;
                }
                virtual int GetInt(){
                        return m_nA;
                }
        private:
                int m_nA;
};
A类{
公众:
A():m_nA(10){
标准::cout
将基类“A”中的上述内容更改为“protected”。
不要在“B类”中定义成员“m_nA”。
就这样!你可以走了

将基类“A”中的上述内容更改为“protected”。 不要在“B类”中定义成员“m_nA”。
就这样!您可以开始了。

虚拟函数是一个不错的选择,但您也可以使用此代码

class A{
public:
    A(){
        std::cout << "Constructor A called" << std::endl;
        m_nA = 10;
    }

    int GetInt(){
        return m_nA;
    }

protected:
    int m_nA;
};

class B : public A{
public:
    B(){
        std::cout << "Constructor B called" << std::endl;
        m_nA = 5;
    }
};
A类{
公众:
(){

std::cout虚拟函数是一个不错的选择,但您也可以使用此代码

class A{
public:
    A(){
        std::cout << "Constructor A called" << std::endl;
        m_nA = 10;
    }

    int GetInt(){
        return m_nA;
    }

protected:
    int m_nA;
};

class B : public A{
public:
    B(){
        std::cout << "Constructor B called" << std::endl;
        m_nA = 5;
    }
};
A类{
公众:
(){

std::不需要在类B中将GetInt再次声明为virtual,一旦在基类中为virtual,该函数在其subclasses@DebasishJana是的,但我更愿意在子类中重复它,使其可见这是一个虚拟方法指针或引用在基类中甚至不必是虚拟的,如果他打算这样使用它的话。不需要在类B中再次将GetInt声明为虚拟的,一旦在基类中是虚拟的,函数在其subclasses@DebasishJana是的,但我更愿意在子类中重复它,以使其可见这是一个virtual方法。特别是,
virtual
根本没有必要,因为OP不通过指针或引用调用它。如果他打算这样使用它,它甚至不必在基类中是虚拟的。