C++ 抽象类与信息丢失
我有一个关于从抽象类创建类以及如何剪切数据的问题 假设我们有一个抽象类,名为Animal,以及实现该类的Cat和Dog。这两个类都实现了一个来自Animal的方法,名为update();但是,在更新方法中,它们访问私有方法和/或变量,这些私有方法和/或变量是它们自己独有的,而不是动物类。我理解如果我们以这种方式声明类C++ 抽象类与信息丢失,c++,abstract,C++,Abstract,我有一个关于从抽象类创建类以及如何剪切数据的问题 假设我们有一个抽象类,名为Animal,以及实现该类的Cat和Dog。这两个类都实现了一个来自Animal的方法,名为update();但是,在更新方法中,它们访问私有方法和/或变量,这些私有方法和/或变量是它们自己独有的,而不是动物类。我理解如果我们以这种方式声明类 Animal* dog = new Dog(); Animal* cat = new Cat(); 我们只能访问只有动物类才具有的方法或变量;但是,如果我对每个类调用update
Animal* dog = new Dog();
Animal* cat = new Cat();
我们只能访问只有动物类才具有的方法或变量;但是,如果我对每个类调用update()方法,而这个更新方法调用Cat和Dog中的独占成员,会怎么样。这是合法的,还是因为我将这些数据创建为动物而将其截断?该语言不会强制您仅使用对方法“update()”的多态访问 例如:
class Animal {
public:
virtual void update() { std::cout << "Animal::update() " << std::endl; }
virtual ~Animal(){}
};
class Cat : public Animal {
public:
virtual ~Cat(){}
virtual void update() { std::cout << "Cat::update() " << std::endl; }
};
class Dog : public Animal {
public:
virtual ~Dog(){}
virtual void update() { std::cout << "Dog::update() " << std::endl; }
};
int t395(void)
{
Animal* dog = new Dog();
Animal* cat = new Cat();
dog->update();
cat->update();
dog->Animal::update();
return (0);
}
// OUTPUT:
// Dog::update()
// Cat::update()
// Animal::update()
类动物{
公众:
virtual void update(){std::cout不确定我是否完全理解您的问题,但您的update()描述了一个虚拟函数调用
我怀疑你理解继承和多态是如何实现的,我知道是这样的。考虑下面的内容:
class base
{
private:
int mylocalInt; //inaccessible to anyone but the base.
protected:
int sharedInt = 5;//accessible by base, and any subtype.
public:
base()
{
cout<<"creating base object"<<endl;
}
virtual ~base()
{
cout<<"Now destroying base object"<<endl;
}
void virtual callMe()//will be overridden, only called if you directly instantiate a base object.
{
cout<<"I am a base"<<endl;
}
};
class subtypeA : public base
{
private:
int Aint;
public:
subtypeA()
{
cout<<"creating subtype"<<endl;
}
~subtypeA()
{
cout<<"destroying subtype"<<endl;
}
void callMe()
{
cout<<"I am a subtypeA"<<endl;
}
int getAint()//this is a local, IE static function, a base ptr cannot access it.
{
return Aint;
}
int getSharedInt()//notice how sharedInt, located in the base object, is still accessible from within the subtype.
{
return sharedInt;
}
};
int main(int argc, char* argv[] )
{
base* ptr = new subtypeA;
ptr->callMe();//works fine, will check which subtype, if any, ptr points to, and call the appropriate callMe(). This is because
//callMe() is virtual in base.
//ptr->sharedInt//illegal, main is not part of base or a subtype of base.
subtypeA* Aptr = (subtypeA*)ptr;//since all pointers are the same size, they can be cast to one another, is dangerous however
cout<<Aptr->getSharedInt()<<endl;//works, getSharedInt is NOT virtual, but a normal static member of subtypeA, so in order to use it, the pointer
//needs to be of type subtypeA. the sharedInt however is protected, so subtypeA can access it due to the fact that it is related to it's owner, base.
}
类基
{
私人:
int mylocalInt;//除基地外,任何人都无法访问。
受保护的:
int sharedInt=5;//可由base和任何子类型访问。
公众:
base()
{
在大多数情况下,你所发布的代码不是有效的C++。你必须先把你的动物赶出来。它们会在动物类的情况下保留它们的信息吗?@ XARI提供了一个或更好的尝试你想要的,并告诉我们结果狗访问->动物::UpDebug()不影响派生类(在这个简单的例子中)。这会截断其他类中的任何数据吗?比如说,Cat在其更新函数中调用独占成员变量是否合法。Cat->update会起作用吗?术语“切片”不适用,因为此继承非常简单。不会截断基类或这些派生类中的任何数据。想象一下想要访问ss(通过多态指针狗)一个方法“void dog::scratchEar();”,但没有计划虚拟访问,因为您决定其他动物没有这种行为。有些人会尝试将动物*狗转换为狗*狗。Yuk,糟糕的程序员。一个负责任的方法是简单地添加“virtual Animal::scratchEar()”,然后“virtual Dog::scratchEar()”,并提供“Animal::scratchEar()”的“不做任何事情”实现,这将满足所有未实现的动物!无需强制转换。不做任何事情很容易。仍然是多态的!