C++一些细节

/*#include<iostream>
#include"pthread.h"

using namespace std;

static pthread_mutex_t* mutex;
class Single;

class Single
{
private:
    Single() {};
public:
    static Single* instance;
    static Single* getInstance()
    {
        if (instance == NULL)
        {
            pthread_mutex_lock(mutex);
            if (instance == NULL)
            {
                instance = new Single();
            }
            pthread_mutex_unlock(mutex);
        }
        else
        {
            cout << "资源已被占用" << endl;
        }
        return instance;
    }
    void display()
    {
        cout << "单例模式" << endl;
    }
};

Single* Single::instance = NULL;

int main() 
{
    pthread_mutex_init(mutex, NULL);
    Single* sing = Single::getInstance();
    sing->display();

    Single* s = Single::getInstance();
    s->display();

    system("pause");
}

class Parent
{
public:
    virtual void foo1(){}
    virtual void foo2(){}
    virtual foo3();
};
class Child1 :public Parent
{
public:
    void foo1(){}
    void foo3();

};

class Child2 :public Parent
{
    void foo1(){}
    void foo2(){}
    void foo3();
};
虚函数表的相关概念,定义的三个类中三个函数的内存地址是不同的,表现除了虚函数具有多态性和继承性的特点。
*/
/*
//构造函数调用虚函数
#include<iostream>

using namespace std;

class A
{
    A() { doSth(); }
    virtual void doSth()
    {
        printf("I an A");
    }
};

class B :public A
{
public:
    virtual void doSth()
    {
        printf("I am B");
    }
};

int main()
{
    A  c;
    system("pause");
}


*/
/*
#include<iostream>
#include<string>

using namespace std;

class Test
{
public:
    char* buf;
    Test(void)
    {
        buf = NULL;
    }
    Test(const char* str)
    {
        buf = new char[strlen(str) + 1];
        strcpy(buf, str);
    }
    Test(Test& test)                        //如果不添加此复制构造函数,t1的buf和t2的buf指向相同的堆内存,为浅复制,加入此复制构造函数,为对象创造了新的内存,指向不同的地址。
    {
        buf = new char[strlen(test.buf) + 1];
        strcpy(buf, test.buf);
    }
    ~Test()
    {
        if (buf != NULL)
        {
            delete buf;
            buf = NULL;
        }
    }
};

int main()
{
    Test t1("hello");
    Test t2 = t1;
    cout << "(t1.buf==t2.buf)?" << (t1.buf == t2.buf) ? "yes" : "no";


    system("pause");
}
*/
/*
#include<iostream>

using namespace std;

class Base
{
    int i;
public:
    Base() :i(0) { cout << "Base()" << endl; }
    Base(int n) :i(n) { cout << "Base(int)" << endl; }
    Base(const Base& b) :i(b.i)
    {
        cout << "Base(Base&)" << endl;
    }
};

class Derverd :public Base
{
    int j;
public:
    Derverd() :Base(0), j(0) { cout << "Deriverd()" << endl; }
    Derverd(int m, int n) :Base(m), j(0) { cout << "Derverd(int)" << endl; }
    Derverd(Derverd &obj) :Base(obj), j(obj.j)
    {
        cout << "Deriverd(Deriverd&)" << endl;
    }
};

int main()
{
    Base b(1);
    Derverd obj(2, 3);
    Derverd d = obj;
    system("pause");
}
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值