Click here to Skip to main content
15,891,943 members

C++ destructor scope call

blackhattrick asked:

Open original thread
Hi
I have got a problem with destructor calls.

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

C++
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

C++
//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

C++
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
C++
class MyClass
{
public:
	List<int> * lstGetList1(void);
private:
	List<int> lstList;	
};

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


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

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



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:

C++
List<MyClass> foo;

//Inserting MyClass objects to foo...

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


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
Tags: C++, Visual Studio (Visual Studio 2010)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900