C++ Class 构造函数什么时候用指针调用?
When is C++ Class Constructor called with pointer?
我对c++了解不深
我已经实现了智能指针来防止由原始指针引起的问题。 (内存泄漏)
智能指针的代码如下
#ifndef SMARTPOINTER
#define SMARTPOINTER
class RC
{
private:
int count; // Reference count
public:
RC(){ count = 0; }
void AddRef()
{
// Increment the reference count
count++;
}
int Release()
{
// Decrement the reference count and
// return the reference count.
if(count==0) return 0;
return --count;
}
};
template < typename T > class SP
{
private:
T* pData; // pointer
RC* reference; // Reference count
public:
SP() : pData(0), reference(0)
{
// Create a new reference
reference = new RC();
// Increment the reference count
reference->AddRef();
}
SP(T* pValue) : pData(pValue), reference(0)
{
// Create a new reference
reference = new RC();
// Increment the reference count
reference->AddRef();
}
SP(const SP<T>& sp) : pData(sp.pData), reference(sp.reference)
{
// Copy constructor
// Copy the data and reference pointer
// and increment the reference count
reference->AddRef();
}
~SP()
{
// Destructor
// Decrement the reference count
// if reference become zero delete the data
if(reference->Release() == 0)
{
delete pData;
delete reference;
}
}
T& operator* ()
{
return *pData;
}
T* operator-> ()
{
return pData;
}
SP<T>& operator = (const SP<T>& sp)
{
// Assignment operator
if (this != &sp) // Avoid self assignment
{
// Decrement the old reference count
// if reference become zero delete the old data
if(reference->Release() == 0)
{
delete pData;
delete reference;
}
// Copy the data and reference pointer
// and increment the reference count
pData = sp.pData;
reference = sp.reference;
reference->AddRef();
}
return *this;
}
};
#endif // SMARTPOINTER
我的问题如下..
使用此智能指针时 class 如下
SP<MyObject> ptrObject; //MyObject class is the custom class.
MyObject *ptr = new MyObject;
ptrObject = ptr; //This invokes the SP(T *) constructor.. why?
//Though operator = ( T*) is not defined.
此时operator=(T*)
函数虽然没有定义,但是为什么调用了智能指针的构造函数SP(T* pValue)?
我认为在创建 class 对象时会调用构造函数。
请解释一下,谢谢。
每当在不接受该类型但接受其他类型 T2 的上下文中使用某种类型 T1 的表达式时,就会执行隐式转换;
特别是:
- 调用以 T2 作为参数声明的函数时,表达式用作参数;
- 当表达式用作期望 T2 的运算符的操作数时;
- 当初始化类型为 T2 的新对象时,包括函数中的 return 语句 returning T2;
- switch语句中使用表达式时(T2为整型);
- 当表达式用于 if 语句或循环时(T2 为布尔值)。
我对c++了解不深
我已经实现了智能指针来防止由原始指针引起的问题。 (内存泄漏)
智能指针的代码如下
#ifndef SMARTPOINTER
#define SMARTPOINTER
class RC
{
private:
int count; // Reference count
public:
RC(){ count = 0; }
void AddRef()
{
// Increment the reference count
count++;
}
int Release()
{
// Decrement the reference count and
// return the reference count.
if(count==0) return 0;
return --count;
}
};
template < typename T > class SP
{
private:
T* pData; // pointer
RC* reference; // Reference count
public:
SP() : pData(0), reference(0)
{
// Create a new reference
reference = new RC();
// Increment the reference count
reference->AddRef();
}
SP(T* pValue) : pData(pValue), reference(0)
{
// Create a new reference
reference = new RC();
// Increment the reference count
reference->AddRef();
}
SP(const SP<T>& sp) : pData(sp.pData), reference(sp.reference)
{
// Copy constructor
// Copy the data and reference pointer
// and increment the reference count
reference->AddRef();
}
~SP()
{
// Destructor
// Decrement the reference count
// if reference become zero delete the data
if(reference->Release() == 0)
{
delete pData;
delete reference;
}
}
T& operator* ()
{
return *pData;
}
T* operator-> ()
{
return pData;
}
SP<T>& operator = (const SP<T>& sp)
{
// Assignment operator
if (this != &sp) // Avoid self assignment
{
// Decrement the old reference count
// if reference become zero delete the old data
if(reference->Release() == 0)
{
delete pData;
delete reference;
}
// Copy the data and reference pointer
// and increment the reference count
pData = sp.pData;
reference = sp.reference;
reference->AddRef();
}
return *this;
}
};
#endif // SMARTPOINTER
我的问题如下..
使用此智能指针时 class 如下
SP<MyObject> ptrObject; //MyObject class is the custom class.
MyObject *ptr = new MyObject;
ptrObject = ptr; //This invokes the SP(T *) constructor.. why?
//Though operator = ( T*) is not defined.
此时operator=(T*)
函数虽然没有定义,但是为什么调用了智能指针的构造函数SP(T* pValue)?
我认为在创建 class 对象时会调用构造函数。
请解释一下,谢谢。
每当在不接受该类型但接受其他类型 T2 的上下文中使用某种类型 T1 的表达式时,就会执行隐式转换;
特别是:
- 调用以 T2 作为参数声明的函数时,表达式用作参数;
- 当表达式用作期望 T2 的运算符的操作数时;
- 当初始化类型为 T2 的新对象时,包括函数中的 return 语句 returning T2;
- switch语句中使用表达式时(T2为整型);
- 当表达式用于 if 语句或循环时(T2 为布尔值)。