简体   繁体   English

我正确地构建这些引用和指针函数吗? 如果是这样,我如何明确地调用我的析构函数?

[英]Am I building these reference and pointer functions correctly? And if so, how do I explicitly call my destructor?

Reference function: 参考功能:

// the getDept() function shall be defined as a reference function. That is, a call to this function will copy the
// member variable m_iDeptID into the int variable referenced by the function argument
void EmployeeRecord::getDept(int& d)
{
    d = m_iDeptID;
}

Pointer function: 指针功能:

// the getSalary() function shall be defined as a pointer function. That is, a call to this function will copy the
// member variable m_dSalary into the int variable pointed to by the function argument
void EmployeeRecord::getSalary(double *sal)
{
    *sal = m_dSalary;
}

Destructor (I don't have any delete statements, or anything, in the function): 析构函数(我在函数中没有任何删除语句或任何东西):

// destructor - cleans up and deallocates any memory that pointers within this class may have referenced to
EmployeeRecord::~EmployeeRecord(){}

My attempt at explicitly calling the destructor: 我尝试显式调用析构函数:

EmployeeRecord Employee1;
Employee1.~EmployeeRecord();

So my questions are: (1) Are my reference and pointer functions consistent with their descriptions? 所以我的问题是:(1)我的引用和指针功能是否与其描述一致? (2) If they are, what code should I put into the block of my destructor so that I can call the destructor explicitly, and it successfully "cleans up and deallocates any memory that pointers within this class may have referenced to" (if there is, in fact, anything I need to put in the body of the destructor)? (2)如果是,我应该将什么代码放入析构函数的块中,以便我可以显式调用析构函数,并且它成功地“清理并释放此类中指针可能引用的任何内存”(如果有的话)事实上,我需要放入析构函数体中的任何东西)?

In turn: 反过来:

  1. getDept(int &d) is consistent with its description, but getSalary(double *d) isn't quite: the description says it should copy the value into the int variable pointed to by the function argument. getDept(int &d)与其描述一致,但getSalary(double *d)并不完全:描述说它应该将值复制到函数参数指向的int变量中。

    getSalary then should take a int * argument, instead of a double * argument, and the method should perform the appropriate cast. 然后getSalary应该采用int *参数,而不是double *参数,并且该方法应该执行适当的getSalary (This does seem a bit of an odd specification though — sure it wasn't meant to say double rather than int?) (这确实看起来有点奇怪 - 确定它不是说双重而不是int?)

  2. From the information at hand, there is nothing to clean up, presuming the member variables are just int s and double s and so on. 从手头的信息来看,没有什么可以清理的,假设成员变量只是intdouble s等等。 Are there any members that are pointers? 有指针的成员吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM