C++ 期末考试小总结

完成了程刚出的期末试卷,感觉难度不小,后来又自己在底下研究了一下一些疑难问题,特总结如下:
1.有关基类、派生类指针相互调用的问题:
大体来说,就是因为sizeof(derived calss) >= sizeof(base class),所以用基类的指针指向派生类object或者pointer都是可以的(这里派生类可以当做基类看待),但是只要是派生类的指针,不管指向基类object还是pointer都是不行的。

# include <iostream>
using std::cout;
using std::endl;

class A{
    public:
        display1(){
            cout << "This is class A" << endl;
        }
};

class B:public A{
    public:
        display2(){
            cout << "This is class B" << endl;
        }
};

int main(){
    A *pa;
    A a;
    B *pb;
    B b;

    pa = pb; //A选项 
    pa ->display1();
    pa ->display2(); //不能调用 

    pb = pa; //B选项 错误 invalid conversion from 'A*' to 'B*' 

    pa = &b; //C选项 
    pa -> display1();
    pa -> display2(); //不能调用 

    pb = &a; //D选项  错误 invalid conversion from 'A*' to 'B*' 
    return 0;
}

2.有关一些复杂的指针函数的表示:
类型1:函数返回类型+(*指针)+(函数形参列表)
short (*p[3]) (int): p是具有3个元素的指针数组,数组每个元素指针指向一个 short foo(int)类型的函数;

类型2:int (*p(int))[3]: p是具有三个元素的整数型指针数组,数组的每个元素指针,都是foo(int)的类型的函数的返回值(一个指针函数),见下:

How to understand the following declaration: void (*foo(char))(int);? In fact, foo is a function, the return type is a function pointer. We can divide this statement into two

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值