Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Returning reference to template class object..

Reply
Thread Tools

Returning reference to template class object..

 
 
divya_rathore_@gmail.com
Guest
Posts: n/a
 
      10-10-2005
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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-10-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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 <list>.

>
> 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 <iostream> and use std::cout.

> 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

 
Reply With Quote
 
 
 
 
divya_rathore_@gmail.com
Guest
Posts: n/a
 
      10-10-2005
>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
Returning a private nested class of a class template ymost@hotmail.com C++ 3 01-10-2009 03:48 PM
problem with a function template returning a private nested class ofa class template ymost@hotmail.com C++ 2 12-28-2008 10:43 AM
Returning nested class within template class from member function. jason.cipriani@gmail.com C++ 15 12-16-2008 04:53 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments