首页 > 编程语言 > 详细

关于C++的疑问剖析

时间:2015-09-04 21:04:01      阅读:330      评论:0      收藏:0      [点我收藏+]

1)众所周知,抽象类是不存在对象的,只提供接口而不提供实现。但是抽象类能不能作为一个类指针,指向其子类的对象呢?

class Interface {
    public:
    virtual void fun() = 0;
};

class Implement: public Interface {
public:
    void fun() {
        cout<<"This is Implement object."<<endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Interface *obj = new Implement();
    obj->fun();
    return 0;
}

  结论:能。抽象类不能拥有自己的类对象,但是,可以定义抽象类指针指向其子类的对象。

2)指向指针的指针  两种最常见的情形:(数组的名字会退化成其首元素的指针。

2.1 指针数组:

class Shape {
public:
    void testFunc() {
        cout<<"Shape::testFunc()"<<endl;
    }
    //...
};

Shape *picture[10];

int _tmain(int argc, _TCHAR* argv[])
{
    Shape **temp = picture;
    Shape *p = *temp;
    p->testFunc();
    return 0;
}

技术分享

2.2 

void scanTo(const char **p, char ch) {
    while (**p && **p != ch) {
        ++*p;
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    char s[] = "hello, World";
    const char *cp = s;
    scanTo(&cp, ,);//将指针cp移动第一个“,”出现的位置
    cout<<*cp<<endl;
    return 0;
}

3)

关于C++的疑问剖析

原文:http://www.cnblogs.com/wiessharling/p/4782324.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!