c++抽象类

来源:互联网 发布:淘宝洛丽塔店铺 编辑:程序博客网 时间:2024/06/03 11:29

#include<iostream>
using namespace std;

class A
{
protected:
   
int i;
public:
    A(
int x):i(x){}
   
virtual int operator!() = 0;
};

class B:public A
{
    A
*p;
public:
    B(
int x, A*y = NULL):A(x){p = y;}
   
int operator!()
    {
       
if(p)
           
return !*p;
       
else
           
return 1;
    }
};


int main()
{
    A
*p1 = new B(1);//p1是否是一个指向属性值=1的指针
    A *p2 = new B(2, (A*)p1);//p2是否是一个指向数据i属性值=2,A*p = p1的指针
    cout<<!*p2<<endl;//*p1非空return !*p;应该是输出2才对啊,实在是新手不明白

   
return 0;
}