Returning reference to template class object..

D

divya_rathore_

Dear All,

Assuming that I have an object of a templated class of Linked List:
LinkedList<int> IntList

which adds entries using a member function like this:
void AppendEntry(T& entry)

and retrieves any entry using:
T* FindEntry(int pos);

I want to achieve the following:

static LinkedList<int> GenerateList()
{
LinkedList<int> IntList;

// Add 10 integers to the linked list.
for (int i = 0; i < 10; i++)
IntList.AppendEntry(i);

return IntList;
}

int main()
{
LinkedList<int> list;
list = GenerateList();

printf("%d\n", *(list.FindEntry(7-1)));
return 0;
}

Now, compilation is fine on vc++6 but on executing obviously, in
main(), after the line:
list = ParseFiberFile();
the memory locations returned get free (all become FE EE). Kindly
resolve this for me. I understand that I am missing on some C++ basics
here.

thanks in advance,
Divya Rathore
 
M

mlimber

Dear All,

Assuming that I have an object of a templated class of Linked List:
LinkedList<int> IntList

Unless you're implementing this for homework, it'd probably be better
to use the linked list provided by the standard library: std::list.
Just #include said:
which adds entries using a member function like this:
void AppendEntry(T& entry)

Should probably be AppendEntry(const T& entry).
and retrieves any entry using:
T* FindEntry(int pos);

Could be

const T& FindEntry( int pos ) const;
T& FindEntry( int pos );

You could also use the std::find algorithm here if you're not already.
That would eliminate the need for the member unless you want to get a
reference rather than an iterator.
I want to achieve the following:

static LinkedList<int> GenerateList()
{
LinkedList<int> IntList;

// Add 10 integers to the linked list.
for (int i = 0; i < 10; i++)
IntList.AppendEntry(i);

return IntList;
}

int main()
{
LinkedList<int> list;
list = GenerateList();

Initializing on declaration is a good idea.

LinkedList<int> list = GenerateList();

But the question is, what does the assignment operator of LinkedList
do? Does it just copy pointers whose pointees are then deleted by the
destructor of the IntList in GenerateList()? I'm guessing you didn't
define your own copy constructor or assignment operator and that you're
getting bitten by the implicitly generated ones.
printf("%d\n", *(list.FindEntry(7-1)));

Prefer iostreams to printf. They're typesafe and C++ friendly. Just
#include said:
return 0;
}

Now, compilation is fine on vc++6 but on executing obviously, in
main(), after the line:
list = ParseFiberFile();
the memory locations returned get free (all become FE EE). Kindly
resolve this for me. I understand that I am missing on some C++ basics
here.

thanks in advance,
Divya Rathore

Cheers! --M
 
D

divya_rathore_

Initializing on declaration is a good idea.
> LinkedList<int> list = GenerateList();
But the question is, what does the assignment operator of LinkedList
do? Does it just copy pointers whose pointees are then deleted by the
destructor of the IntList in GenerateList()? I'm guessing you didn't
define your own copy constructor or assignment operator and that you're
getting bitten by the implicitly generated ones.

Hmm.. I am getting the idea. I was not using STL deliberately.
I was more interested in knowing the hows and whys of static returns.
Seems copy ctrs and assignment ops are indeed needed.
Thanks! You were helpful :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top