C++ "coyp constructor" and webkit's RefPtr useage

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());


output:

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..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值