宏,函數模版方式實現繼承方案

來源:互聯網
上載者:User

在QT中,對於所有定義了訊號和槽的類,定義的開始處需要加入:Q_OBJECT宏!!!

/* tmake ignore Q_OBJECT */#define Q_OBJECT \public: \    Q_OBJECT_CHECK \    static const QMetaObject staticMetaObject; \    Q_OBJECT_GETSTATICMETAOBJECT \    virtual const QMetaObject *metaObject() const; \    virtual void *qt_metacast(const char *); \    QT_TR_FUNCTIONS \    virtual int qt_metacall(QMetaObject::Call, int, void **); \private: \    Q_DECL_HIDDEN static const QMetaObjectExtraData staticMetaObjectExtraData; \    Q_DECL_HIDDEN static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **);

 

由於眾所周知的,繼承將導致記憶體及效能消耗,這種方式是個不錯的選擇.下面,我們靈活的運用這個方案.

 

/*  * File:   main.cpp * Author: Vicky.H * Email:  eclipser@163.com */#include <iostream>#include <typeinfo>#include <string.h>class A {public:    virtual ~A() {};    virtual void sayHello() = 0;};class A1 : public A {public:    void sayHello() {        std::cout << "hello A1" << std::endl;    }    ~A1() {        std::cout << "delete A1" << std::endl;    }};class A2 : public A {public:    void sayHello() {        std::cout << "hello A2" << std::endl;    }    ~A2() {        std::cout << "delete A2" << std::endl;    }};#define B_OBJECT                                                                \public:                                                                         \void sayHello() {                                                               \    std::cout << "hello " << this->name << "\t"                                 \        << typeid(this).name()  << std::endl;                                   \}                                                                               \private:                                                                        \    char name[21];                                                              template<typename T>void sayHello (T& o) {    o.sayHello();}template<typename T>void sayHello (T* o) {    o->sayHello();}template<typename T>void sayHello (const T* o) {    o->sayHello();}class B1 {    B_OBJECTpublic:    B1(const char* name) {        strcpy(this->name,name);    }};class B2 {    B_OBJECTpublic:    B2(const char* name) {        strcpy(this->name,name);    }};/* *  */int main(void) {        A* a1 = new A1;    a1->sayHello();    delete a1;        A* a2 = new A2;    a2->sayHello();    delete a2;        std::cout << "---------------------------" << std::endl;        B1 *b1 = new B1("Vicky");    b1->sayHello();    sayHello(b1);    delete b1;        B2 *b2 = new B2("Jack");    b2->sayHello();    sayHello(b2);    delete b2;        return 0;}

 

hello A1
delete A1
hello A2
delete A2
---------------------------
hello Vicky     P2B1
hello Vicky     P2B1
hello Jack      P2B2
hello Jack      P2B2

運行 SUCCESSFUL (總時間:  63ms)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.