在构造函数中使用成员函数 [英] use of member functions in constructors

查看:93
本文介绍了在构造函数中使用成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的队列类。是否可以从构造函数中调用私有成员函数




//自定义构造函数

MyQueue: :MyQueue(unsigned long lInitialSize,

unsigned long lInitialSizeAlignment)

{

this-> pbQueue = NULL;

this-> InitializeClass(lInitialSize,lInitialSizeAlignment);

}

//默认构造函数

MyQueue :: MyQueue()

{

this-> pbQueue = NULL;

this-> InitializeClass(DEFAULT_SIZE,DEFAULT_SIZE_ALIGNMENT);

} $ / $

//处理构造任务

void

MyQueue :: InitializeClass(unsigned long lInitialSize,

unsigned long lInitialSizeAlignment)

{

if(pbQueue!= NULL)

{

return;

}

if(lInitialSize == 0)

{

lInitialSize = DEFAULT_SIZE;

}

if(lInitialSizeAlignment = 0)

{

lInitialSizeAlignment = DEFAULT_SIZE_ALIGNMENT;

}


this-> pbQueue =(LPBYTE)HeapAlloc(hProcessHeap,HEAP_ZERO_MEMORY,

lInitialSize) ;

CheckAllocation(this-> pbQueue,1);


this-> pbFront = this-> pbQueue;

this-> lQueueLength = 0;

this-> lQueueMax = lInitialSize;

this-> lSizeAlignment = lInitialSizeAlignment;

}

Here is my queue class. Is it okay to call a private member function
of the class from the constructor?

// custom constructor
MyQueue::MyQueue(unsigned long lInitialSize,
unsigned long lInitialSizeAlignment)
{
this->pbQueue = NULL;
this->InitializeClass(lInitialSize, lInitialSizeAlignment);
}
// default constructor
MyQueue::MyQueue()
{
this->pbQueue = NULL;
this->InitializeClass(DEFAULT_SIZE, DEFAULT_SIZE_ALIGNMENT);
}

// handles construction task
void
MyQueue::InitializeClass(unsigned long lInitialSize,
unsigned long lInitialSizeAlignment)
{
if(pbQueue != NULL)
{
return;
}
if(lInitialSize == 0)
{
lInitialSize = DEFAULT_SIZE;
}
if(lInitialSizeAlignment = 0)
{
lInitialSizeAlignment = DEFAULT_SIZE_ALIGNMENT;
}

this->pbQueue = (LPBYTE) HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY,
lInitialSize);
CheckAllocation(this->pbQueue, 1);

this->pbFront = this->pbQueue;
this->lQueueLength = 0;
this->lQueueMax = lInitialSize;
this->lSizeAlignment = lInitialSizeAlignment;
}

推荐答案

是否可以从构造函数中调用类的私有成员函数?
Is it okay to call a private member function
of the class from the constructor?



是的。您可以从以下位置调用类的私有成员函数:


A)在类的成员函数内(不一定是当前的

对象)。这包括来自任何构造函数和析构函数。


B)朋友函数和朋友类的成员函数。

-JKop




" Shailesh Humbad" <无***** @ nowhere.com>在消息中写道

"Shailesh Humbad" <no*****@nowhere.com> wrote in message
这是我的队列类。是否可以从构造函数中调用类的私有成员函数?
Here is my queue class. Is it okay to call a private member function
of the class from the constructor?




是的。实际上,这是一种在重载的

构造函数之间共享通用功能的方法。另请注意,构造函数内部未实现虚拟化。


Sharad



Yes. In fact this is a way to share common functionality between overloaded
constructors. Also note that virtuality is not achieved inside constructors.

Sharad


Sharad Kala写道:
Sharad Kala wrote:
Shailesh Humbad <无***** @ nowhere.com>在消息中写道

"Shailesh Humbad" <no*****@nowhere.com> wrote in message

这是我的队列类。可以从构造函数中调用类的私有成员函数吗?
Here is my queue class. Is it okay to call a private member function
of the class from the constructor?



是的。实际上,这是一种在重载的构造函数之间共享通用功能的方法。另请注意,构造函数内部未实现虚拟化。

Sharad


Yes. In fact this is a way to share common functionality between overloaded
constructors. Also note that virtuality is not achieved inside constructors.

Sharad




我发现了一个错误,因为我做了以下操作在我的构造函数中


MyObject :: MyObject(){

//可能会导致错误,即风格不佳

ZeroMemory(这个,sizeof(this));

...

}


工作正常,直到我添加了STL列表到MyObject。然后我在一个不相关的部分

代码中的begin()函数调用中得到了访问冲突

。最后,我追踪它并意识到STL列表的所有内部指标都被重置为零(内存泄漏)。这个

的bug让我有点害怕从构造函数中访问正在构造的对象

,这就是我问这个问题的原因。所以我的

问题也是关于好的风格。



I just found a bug because I had done the following in my constructor

MyObject::MyObject() {
// may cause bugs later, i.e. poor style
ZeroMemory(this, sizeof(this));
...
}

It was working fine until I added an STL list to MyObject. Then I got
an access violation in a begin() function call in an unrelated piece
of code. Eventually, I tracked it down and realized all the internal
pointers of the STL list were being reset to zero (memory leak). This
bug just scared me a little to access the object being constructed
from within the constructor, which is why I asked the question. So my
question is about good style as well.


这篇关于在构造函数中使用成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆