C ++析构函数范围调用 [英] C++ destructor scope call

查看:79
本文介绍了C ++析构函数范围调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


析构函数调用有问题.

我有一个实现链表的类,如下所示:

Hi
I have got a problem with destructor calls.

I have a class which implements a linked list like this:

template <class NODETYPE>
class List
{
public:
	List();
	~List();
	void vdInsertFront(const NODETYPE &);
	void vdInsertLast(const NODETYPE &);
	int inDeleteFront(void);
	int inDeleteLast(void);
	bool blIsEmpty();
	void vdPrint();
	int vdShuffle(void);
	int inGetLenght();
	NODETYPE * inObtenerDatoNodo(int);

private:
	int inLenght;
	Node<NODETYPE> *ptrFirst;
	Node<NODETYPE> *ptrLast;
	Node<NODETYPE> *ptrNewNode(const NODETYPE &);
};



这是它的构造函数和析构函数



This is its constructor and destructor

//Constructor
template <class NODETYPE>
List<NODETYPE>::List()
{
	ptrFirst = 0;
	ptrLast = 0;
}

//Destructor
template <class NODETYPE>
List<NODETYPE>::~List()
{
	if(!blEmpty())
	{
		Nodo<NODETYPE> * ptrActual = ptrFirst;
		Nodo<NODETYPE> * ptrAux;

		while(ptrActual != 0)
		{
			ptrAux = ptrActual;
			ptrActual = ptrActual->ptrNext;
			delete ptrAux;
		}
	}
}

//Some functions bodies
.
.
.


该函数返回一个指向列表中特定元素的指针


This function returns a pointer to an especific element of the list

template <class NODETYPE>
NODETYPE * List<NODETYPE>::inGetNodeData(int index)
{
	Nodo<NODETYPE> * ptrAux = ptrFirst;
	for(int i = 0; i <= index - 1; i++)
		ptrAux = ptrAux->ptrNext;

	return &ptrAux->_data;
}



现在,我有另一个类,该类具有作为私有成员的列表对象和
返回列表指针的函数



Now I have another class that has a list object as a private member and
a function that returns a pointer of the list

class MyClass
{
public:
	List<int> * lstGetList1(void);
private:
	List<int> lstList;	
};

List<int> * foo::lstGetList1(void)
{
	return &lstList;
}



现在,如果在这样的foo对象中调用成员列表的函数vdPrint:



Now, if I call the function vdPrint of the member list in a foo object like this:

MyClass foo;
(foo.lstGetList1())->vdPrint();




析构函数被调用,当我不想这样做时删除我的列表.

此示例中的代码非常简化了我的代码.实际上,我正在创建MyClass对象的列表,并且代码或多或少像这样:




The destructor is called, deleting my list when I don''t want to do that.

The code in this example is very simplified of what I have. In fact I''m creating a list of MyClass objects and the the code is more or less like this:

List<MyClass> foo;

//Inserting MyClass objects to foo...

((foo.inGetNodeData(1))->lstGetList1())->vdPrint();



我认为析构函数之所以被调用是因为我创建了一个匿名"列表对象,调用了vdPrint方法,然后,因为下一行代码超出了匿名列表范围,所以析构函数被自动调用.如何避免这种情况?


在此先感谢

Ivan



I think the destructor its been called because Im creating an "anonymous" list object, calling vdPrint method, an then, because the next line code is beyond the anonymous list scope, the destructor is called automatically. How can I avoid this?


Thanks in advance

Ivan

推荐答案

MyClass foo;
(foo.lstGetList1())->vdPrint();



调用vdPrint()不会删除您的列表.
在这里,您创建了MyClass的本地对象.

我希望vdPrint()的实现是打印元素,并且不会破坏对象.

lstGetList1()返回List的地址.
inGetNodeData还返回List的地址.

但是foo是一个本地对象,它将在其作用域之后销毁.
请尝试在堆中分配MyClass对象,并在不需要时销毁它.



Calling vdPrint() will not delete your list.
Here you created a local object of MyClass.

I hope implementation of vdPrint() is to print the elements, and it will not destroy the object.

lstGetList1() returns the address of List.
inGetNodeData also returns the adress of List.

But foo is a local object, and it will be destroyed after its scope.
Please try to allocate object of MyClass in heap and destroy it whenever not needed.

MyList foo = new MyList;
(foo->lstGetList1())->vdPrint();


尝试将const添加到inGetNodeData或lstGetList1中,看看是否有区别
try to add const to either inGetNodeData or lstGetList1 and see if it makes difference


这篇关于C ++析构函数范围调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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