C++ constructure and destructure
Two places to call the default construture function.
1)it will call the default copy constructure while passing the parameters.
2).it will also call the default copy construture when return an object.
a. call in both
#include <iostream>
using namespace std;
int g_id = 0;
class CORANGE
{
public:
CORANGE()
{
m_id = g_id++;
printf("default constructure called id=%d\n",m_id);
}
CORANGE(const CORANGE & old)
{
m_id = g_id++;
printf("default copy constructure called id=%d\n",m_id);
}
int getId() const
{
return m_id;
}
~CORANGE()
{
printf("default copy destructure called id=%d\n",m_id);
}
protected:
private:
int m_id;
};
void noReturn(CORANGE inOrange)
{
//inOrange.getId();
return;
}
void printID(CORANGE orange)
{
cout<<orange.getId();
}
CORANGE copy(CORANGE oldOrange)
{
return oldOrange;
}
int main()
{
int i = 0;
CORANGE orange;
copy(orange);
//printID(orange);
//getchar();
}
output:
default constructure called id=0
default copy constructure called id=1
default copy constructure called id=2
default copy destructure called id=1
default copy destructure called id=2
default copy destructure called id=0
b. call in passing parameter
int main()
{
int i = 0;
CORANGE orange;
noReturn(orange);
//printID(orange);
getchar();
}
output:
default constructure called id=0
default copy constructure called id=1
default copy destructure called id=1
default copy destructure called id=0
c. call in return value
CORANGE g_orange;
void printID(CORANGE orange)
{
cout<<orange.getId();
}
CORANGE copy(CORANGE oldOrange)
{
return oldOrange;
}
void noReturn(CORANGE inOrange)
{
//inOrange.getId();
return;
}
CORANGE onlyReturn ()
{
return g_orange;
}
int main()
{
int i = 0;
onlyReturn();
//CORANGE orange;
//noReturn(orange);
//printID(orange);
getchar();
}
output
default constructure called id=0
default copy constructure called id=1
default copy destructure called id=1
default copy destructure called id=0
class B
{
public:
B(){cout<<"B default constructure."<<endl;}
B(const B &b){cout<<"B copy constructure."<<endl;}
B&operator=(const B &b){cout<<"= constructure called";return *this/*b*/; }
};
int _tmain(int argc, _TCHAR* argv[])
{
B b;
B b2 = b;
return 0;
}
result
B default constructure.B copy constructure.
B b2 = b;// will call copy constructure not evaluate constructure.
count churn
1. raw pointer
class CLyftest : public RefCounted<CLyftest> {
public:
static PassRefPtr<CLyftest> create() { return adoptRef(new CLyftest()); }
CLyftest():m_id(20)
{};
private:
int m_id;
};
int testRefCount(CLyftest * test)
{
int count = test->refCount();
return count;
}
int main(int argc, char* argv[])
{
printf("lyftest ok\n");
RefPtr<CLyftest> lyftest= CLyftest::create();
int count = lyftest->refCount();
printf("lyftest after create %d\n", lyftest->refCount());
RefPtr<CLyftest> test2 = lyftest;
count = lyftest->refCount();
printf("lyftest after value %d\n", lyftest->refCount());
testRefCount(lyftest.get());
count = lyftest->refCount();
printf("lyftest after call %d\n", lyftest->refCount());
}
output: lyftest ok
lyftest after create 1
lyftest after value 2
lyftest after call 2
using the raw pointer of course won't change the refCount of the pointer.
2. count churn
int testRefPtrCount(RefPtr<CLyftest> ptr)
{
int count = ptr->refCount();
printf("lyftest in function %d\n", ptr->refCount());
return 0;
}
printf("lyftest ok\n");
RefPtr<CLyftest> lyftest= CLyftest::create();
int count = lyftest->refCount();
printf("lyftest after create %d\n", lyftest->refCount());
RefPtr<CLyftest> test2 = lyftest;
count = lyftest->refCount();
printf("lyftest after value %d\n", lyftest->refCount());
testRefPtrCount(lyftest);
count = lyftest->refCount();
printf("lyftest after call %d\n", lyftest->refCount());
output:
lyftest ok
lyftest after create 1
lyftest after value 2
lyftest in function 3
lyftest after call 2
3. PassRefPtr to reduce churn
int testPassRefPtrCount(PassRefPtr<CLyftest> ptr)
{
int count = ptr->refCount();
printf("lyftest in function %d\n", ptr->refCount());
return 0;
}
printf("lyftest ok\n");
RefPtr<CLyftest> lyftest= CLyftest::create();
int count = lyftest->refCount();
printf("lyftest after create %d\n", lyftest->refCount());
RefPtr<CLyftest> test2 = lyftest;
count = lyftest->refCount();
printf("lyftest after value %d\n", lyftest->refCount());
testPassRefPtrCount(lyftest.release());
count = lyftest->refCount();
printf("lyftest after call %d\n", lyftest->refCount());
lyftest ok
lyftest after create 1
lyftest after value 2
lyftest in function 2
But it will crash as execute "count = lyftest->refCount;" I am still finding out..