Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++如何访问基类继承和重写属性的值?_C++_Oop_Inheritance - Fatal编程技术网

C++如何访问基类继承和重写属性的值?

C++如何访问基类继承和重写属性的值?,c++,oop,inheritance,C++,Oop,Inheritance,我尝试这样做: class A{ public: A(){number = 1;} int number; }; class B : public A{ public: B(){number = 2;} }; class Base { public: Base() {myAttribute = new A();} int returnAttrNumber(){return myAttribute->number;} A *myAttribu

我尝试这样做:

class A{
public:
    A(){number = 1;}
    int number;
};
class B : public A{
public:
    B(){number = 2;}
};

class Base {
public:
    Base() {myAttribute = new A();}
    int returnAttrNumber(){return myAttribute->number;}
    A *myAttribute;
};

class Inherited : public Base{
public:
    Inherited(){myAttribute = new B();}
    B *myAttribute;
};


int main()
{
    Inherited *i = new Inherited();
    std::cout << i->returnAttrNumber(); // outputs 1, because it gets the A not the B. I want it to output 2, to get the B object in returnAttrNumber()
}
因此,基类持有一个对象A。继承持有一个派生自A的对象B。我尝试调用基类上的一个方法,但我希望它尽可能地在对应对象的hirarch中强制转换,而不使用静态或动态强制转换,然后接受B对象,而不是A,并在本例中执行返回其编号的操作

<>有没有一种方法可以在C++中使用基类,而不会有很大的困难? 谢谢你的回答

这是非常糟糕的设计。快速的答案是您可以通过完全限定标识符从基类访问变量。以以下为例:

#include <iostream>

class A
{
public:    
    A()
    : var(1) {}

protected:
    int var;
};


class B : public A
{    
public:
    B()
    : var(2) {}

    int getBVar() const
    {
        return var;
    }

    int getAVar() const
    {
        return A::var;
    }

private:
    int var;
};

int main()
{    
    B b;
    std::cout << "A: " << b.getAVar() << std::endl;
    std::cout << "B: " << b.getBVar() << std::endl;
}
关于下浇铸钻头。。。Base和Inherited有不同的变量。你不可能安全地将一个案例放到另一个案例中。

正如里奥基所说

Base和Inherited有不同的变量

这是因为我在Inherited中将MyAttribute重新声明为B。这是个错误。我想,当我用相同的名称声明它时,它将是相同的变量,这是错误的。 因此,整个解决方案是在继承中取消注释这一行。工作代码:

class A{
public:
    A(){number = 1;}
    int number;
};
class B : public A{
public:
    B(){number = 2;}
};

class Base {
public:
    Base() {myAttribute = new A();}
    int returnAttrNumber(){return myAttribute->number;}
    A *myAttribute;
};

class Inherited : public Base{
public:
    Inherited(){myAttribute = new B();}
    //B *myAttribute;
};


int main()
{
    Base *i = new Inherited(); // this works, what is necessary in my case
    std::cout << i->returnAttrNumber(); // outputs 2 now
}

请张贴一些现实的代码。细节很重要。这应该更改存储在继承中的值,而不是在基中。只有一个成员可以更改其值。请不要使用幻想代码。这有太多的语法错误,分散了对实际问题的注意力是的,对不起,我修复了它,现在它可以工作了整个程序中只有一个int变量…class Base{virtual int get_myAttribute=0;[…]};类派生的{int myAttribute=10;int get_myAttribute override{return myAttribute;}};好的,谢谢!这是可能的,尽管这并不好,因为我程序中的hirarchy包含很多类。这对我来说似乎不是一个非常具体的问题,所以我想知道,为什么我不能放弃,或者为什么对象不能给出提示,它在基类中属于哪种继承类型。。。你说的非常糟糕的设计到底是什么意思?有没有更好的方法来实现我的例子中那样的并行Hirarch呢?因为你在给myAttribute添加别名。最初,它们都指向同一个对象,但让我们想象一个将myArrtibute更改为另一个实例的场景。它对B的实现一无所知,因此无法更新。现在两个指针都指向不同的对象。
class A{
public:
    A(){number = 1;}
    int number;
};
class B : public A{
public:
    B(){number = 2;}
};

class Base {
public:
    Base() {myAttribute = new A();}
    int returnAttrNumber(){return myAttribute->number;}
    A *myAttribute;
};

class Inherited : public Base{
public:
    Inherited(){myAttribute = new B();}
    //B *myAttribute;
};


int main()
{
    Base *i = new Inherited(); // this works, what is necessary in my case
    std::cout << i->returnAttrNumber(); // outputs 2 now
}