Connecting Tech Pros Worldwide Forums | Help | Site Map

Returning reference to template class object..

divya_rathore_@gmail.com
Guest
 
Posts: n/a
#1: Oct 10 '05
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


mlimber
Guest
 
Posts: n/a
#2: Oct 10 '05

re: Returning reference to template class object..


divya_rathore_@gmail.com wrote:[color=blue]
> Dear All,
>
> Assuming that I have an object of a templated class of Linked List:
> LinkedList<int> IntList[/color]

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 <list>.
[color=blue]
>
> which adds entries using a member function like this:
> void AppendEntry(T& entry)[/color]

Should probably be AppendEntry(const T& entry).
[color=blue]
>
> and retrieves any entry using:
> T* FindEntry(int pos);[/color]

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.
[color=blue]
>
> 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();[/color]

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.
[color=blue]
>
> printf("%d\n", *(list.FindEntry(7-1)));[/color]

Prefer iostreams to printf. They're typesafe and C++ friendly. Just
#include <iostream> and use std::cout.
[color=blue]
> 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[/color]

Cheers! --M

divya_rathore_@gmail.com
Guest
 
Posts: n/a
#3: Oct 10 '05

re: Returning reference to template class object..


>Initializing on declaration is a good idea.
[color=blue]
> LinkedList<int> list = GenerateList();[/color]
[color=blue]
>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.[/color]

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 :)

Closed Thread