Returning reference to template class object..

Discussion in 'C++' started by divya_rathore_@gmail.com, Oct 10, 2005.

  1. Guest

    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
    , Oct 10, 2005
    #1
    1. Advertising

  2. mlimber Guest

    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
    mlimber, Oct 10, 2005
    #2
    1. Advertising

  3. Guest

    >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 :)
    , Oct 10, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christopher diggins
    Replies:
    16
    Views:
    730
    Pete Becker
    May 4, 2005
  2. Replies:
    1
    Views:
    320
    John Harrison
    Feb 21, 2007
  3. Replies:
    2
    Views:
    528
  4. A L
    Replies:
    1
    Views:
    503
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  5. JustMe
    Replies:
    1
    Views:
    164
    Tassilo v. Parseval
    Aug 29, 2003
Loading...

Share This Page