Template Operator Overloading with Objects?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Template Operator Overloading with Objects?

  1. #1
    Join Date
    Apr 2017
    Posts
    6

    Template Operator Overloading with Objects?

    Hi I need some help on how to make a copy constructor for this class that uses a list of pointers to a base class objects: here is base class which 2 other classes are derived from it(I did not include them in the code since they are not part of my problem)

    Code:
    ifndef BASEBALLPLAYER_H
    #define BASEBALLPLAYER_H
    
    #include <iostream>
    
    using namespace std;
    
    class BaseBallPlayer{                                           //abstract class meaning no instance can be made for it, which is why we access it via derived classes
    
                                                                    //data members
    protected:
        string name;
        int height;
        int weight;
    
    
    public:
    
        BaseBallPlayer();
        BaseBallPlayer(string nam, int wight, int hight);
        string get_Name();
        virtual void print_player()=0;                                      // 
        virtual void load_player(ifstream & read) = 0;                      // 
    
        virtual ~BaseBallPlayer();                                          
    };
    This is my ArrayList.h File
    Code:
    #ifndef ARRAY_LIST_H
    #define ARRAY_LIST_H
    #include <string>
    
    using namespace std;
    const static int MAX_INIT = 99;
    
        template <class elemType>
        class ArrayList {
        private:
            int n_element;
            elemType * data;
    
        public:
            ~ArrayList();
            ArrayList(int n = MAX_INIT);
            ArrayList(const ArrayList<elemType> &);
            const ArrayList<elemType> & operator=(const ArrayList<elemType> &);
            void MakeEmpty();
            bool IsFull() const;
            int  LengthIs() const;
            void RetrieveItem(elemType &, bool&);
            void InsertItem(elemType);
            void DeleteItem(elemType);
            void ResetList();
            bool IsLastItem();
            void GetNextItem(elemType &);
        };
    This is the standalone class that needs the copy constructor:
    Code:
    #ifndef PLAYERDATABASE_H
    #define PLAYERDATABASE_H
    
    #include <iostream>
    #include "BaseBallPlayer.h"
    #include "ArrayList.h"
    
    using namespace std;
    
        class PlayerDatabase{
    
        private:
            ArrayList<BaseBallPlayer *> teamArrayList; 
    
        public:
    
        };
        #endif
    Since I will be using ArrayList<BaseBallPlayer *> teamArrayList to acces functions in my base and derived classes how would I make a copy constructor for this class?

  2. #2
    Join Date
    Apr 2017
    Posts
    6

    Re: Template Operator Overloading with Objects?

    I am really sorry I dont need a copy constructor, I made a mistake I needed help making an overloaded assignment operator, I dont know what I was thinking when I posted. I needed help with overloading a copy constructor for the standalone class?

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,789

    Re: Template Operator Overloading with Objects?

    So you have a correct copy constructor for class ArrayList and you want help with the assignment operator for class ArrayList - yes?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

  4. #4
    Join Date
    Apr 2017
    Posts
    6

    Re: Template Operator Overloading with Objects?

    Yes
    I just need help overloading the assignment operator for the standalone PlayerDatabase class thats using the ArrayList<BaseBallPlayer *> teamArrayList?
    Last edited by kloud; April 15th, 2017 at 12:22 PM.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,789

    Re: Template Operator Overloading with Objects?

    The assignment operator is really easy. Consider

    Code:
    ArrayList<elemType>& operator=(ArrayList<elemType> rhs)
    {
        swap(rhs.n_element, n_element);
        swap(rhs.data, data);
        return *this;
    }
    and that's it! Note the change to the function definition.

    Think about this carefully so you understand what it's doing.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

  6. #6
    Join Date
    Apr 2017
    Posts
    6

    Re: Template Operator Overloading with Objects?

    I do not understand how I would do this for the PlayerDatabase class. The arraylist.h file i understand but not for the PlayerDatabase class since it has the private ArrayList<BaseBallPlayer *> teamArrayList? That code you posted is for the ArrayList.h file not the PlayerDatabase class I am referring to.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,789

    Re: Template Operator Overloading with Objects?

    PlayerDatabase class has only the one member of type ArrayList<BaseBallPlayer *>. As this type has a correct copy constructor and assignment, then for PlayerDatabase the default copy constructor and default assignment will work as the PlayerDatabase class itself does not do any dynamic memory operations. So

    Code:
    class PlayerDatabase{
    
        private:
            ArrayList<BaseBallPlayer *> teamArrayList; 
    
        public:
           PlayerDatabase() = default;
           PlayerDatabase(const PlayerDatabase&) = default;
           PlayerDatabase& operator=(const PlayerDatabase&) = default;
        };
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,789

    Re: Template Operator Overloading with Objects?

    It would also be better if
    Code:
    ArrayList<BaseBallPlayer *> teamArrayList;
    is
    Code:
    ArrayList<shared_ptr<BaseBallPlayer>> teamArrayList;
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

  9. #9
    Join Date
    Apr 2017
    Posts
    6

    Re: Template Operator Overloading with Objects?

    The copy constructor will be doing dynamic memory operations, because I have a file that has records that will be read into a dynamic array. And ArrayList.h provides the functions to manipulate data in that array. Also what is the = dafult that you implemented?

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,789

    Re: Template Operator Overloading with Objects?

    Can you post the code for the copy constructor.

    The =default just means that the compiler should do a default operation. ie for copy constructor do a shallow copy. Unless dynamic memory is involved this is usually all that's needed.

    From the sample code provided, it didn't look like PlayerDatabase class involved dynamic memory - it was just storing a pointer provided, with the dynamic memory operations in the ArrayList class. Perhaps you could post code as to how this class is used.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

  11. #11
    Join Date
    Apr 2017
    Posts
    6

    Re: Template Operator Overloading with Objects?

    Quote Originally Posted by 2kaud View Post
    Can you post the code for the copy constructor.

    The =default just means that the compiler should do a default operation. ie for copy constructor do a shallow copy. Unless dynamic memory is involved this is usually all that's needed.

    From the sample code provided, it didn't look like PlayerDatabase class involved dynamic memory - it was just storing a pointer provided, with the dynamic memory operations in the ArrayList class. Perhaps you could post code as to how this class is used.

    In my PlayerDatabase.h:
    Code:
    PlayerDatabase(ArrayList<BaseBallPlayer *> players);
    In PlayerDatabase.cpp
    Code:
    PlayerDatabase::PlayerDatabase(ArrayList<BaseBallPlayer *> players){
    	teamArrayList = players;
    }
    Basically my professor tells me that FOR EXAMPLE,
    If we had:
    Code:
    void foo() {
      ArrayList<int *> list;
      ....
    and it tries to insert an int pointers into the list
    Code:
    int * x = new int(5);
    list.InsertItem(x);
    now you do a list copy
    Code:
    ArrayList<int *> list_copy = list;
    Sure, the list_copy has its own dynamic array which is a deep copy of the dynamic array of list, which means the pointer value is copied over, but how about the value being pointed? Thats why we need an overloaded assignment operate that can deal with dynamic memory, with Templated functions.
    Last edited by 2kaud; April 18th, 2017 at 08:47 AM.

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,789

    Re: Template Operator Overloading with Objects?

    list_copy would be a copy of list and the memory locations copied would be the same. So the memory locations held in list would be the same as those held in list_copy. So in the example list_copy and list would both reference the same memory location of x. This is what would be expected in this case.

    If you mean that when you do a copy/assignment the memory address is not copied but a new memory address is generated for the copy and the value copied over then this can be programmed but wouldn't be usual. This would still be done in the copy constructor for ArrayList with the appropriate change in the destructor.

    Note that as far as using ArrayList<> is concerned,
    Code:
    ArrayList<int *> plist;
    int *x = new int(5);
    plist.InsertItem(x);
    ArrayList<int *> plist_copy = plist;
    and
    Code:
    ArrayList<int> list;
    list.InsertItem(5);
    ArrayList<int> list_copy = list;
    don't require any changes to how ArrayList is used. Any special behaviour re storing pointers in ArrayList as opposed to non-pointer types is handled within the ArrayList class.

    If changes to ArrayList are not wanted, then the alternative for the copy is to iterate through the ArrayList, for each element obtain the value stored at the pointer, allocate new memory, store the obtained value at that new memory location and then insert this new element into the new ArrayList. The destructor will also need to be changed.
    Last edited by 2kaud; April 18th, 2017 at 11:31 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)