/*#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");
}
*/
C++一些细节
最新推荐文章于 2024-05-09 16:30:17 发布