Thread: Copy Construcor issue

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    14

    Copy Construcor issue

    Hi!

    This is my code

    Code:
    //Main
    
    #include "Class_header.h"
    #include <iostream>
    using namespace std;
    
    
    
    
    int main ()
    {
    
    
    vectorOfint some_array;
    
    
    some_array.print();
    
    
    vectorOfint copy_of=some_array;         //Copying class
    
    
    copy_of.print();
    
    
    }
    
    // defining methods
    
    #include "Class_header.h"
    #include <iostream>
    using namespace std;
    
    
    
    
    vectorOfint::vectorOfint()    //Constructor
    {
    for (int i=0;i<32;i++)
    {
    p_array[i]=i;
    }
    
    
    }
    
    
    vectorOfint::vectorOfint(const vectorOfint& other) //Copy constructor
    
    
    {
    int *p_array=new int[32];
    x=4;
    
    
    for (int i=0;i<32;i++)
    {
    p_array[i]=other.p_array[i];
    }
    }
    
    
    
    
    
    
    void vectorOfint::print()
    {
    for (int i=0;i<32;i++)
    {
    cout<<p_array[i]<<" ";
    }
    cout<<"\nx is "<<x<<'\n'<<'\n';
    }
    
    //Header
    
    class vectorOfint
    {
    
    
    public:
    
    
    vectorOfint();                          //Constructor
    vectorOfint(const vectorOfint& other);  //Copy constructor
    void print ();
    
    
    private:
    
    
    int *p_array=new int[32];
    int x=0;
    };
    Problem is that if I call some_array.print(), it prints out numbers 0-31 and "x is 0" as it should. But if I call copy_of.print(); it prints out 32 random numbers and "x=4". It changes x but can´t copy members of array. Why?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why?
    Why are you trying to reallocate memory for your array inside the constructor? You've already allocated memory for the array in the class definition.


    Why are you even using dynamic memory instead of a "normal" statically allocated array since you're always seem to be trying to create an array of 32 elements?

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    He's not just reallocating the space, but assigning the new pointer to a local variable.

  4. #4
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    With this code I just wanted to see how copy constructor works (I´m total beginner). As I understand, there is meaning to use copy constructor only if you use pointers and book that I read and videos I watched showed that you should allocate new memory for pointers. My English is not very good so I can't say completely what I mean but I hope you understand enough.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    As I understand, there is meaning to use copy constructor only if you use pointers and book that I read and videos I watched showed that you should allocate new memory for pointers.
    That may be true, but you're already allocating memory every time you create an instance of the class.

    Code:
    class vectorOfint
    {
     
    ...
       private:
          int *p_array=new int[32]; // This allocates memory every time you create an instance of the class.
    And because you manually allocate memory for that pointer every time you create an instance of the class you need to write a destructor, copy constructor and assignment operator, and possibly the move copy consturctor and move assignment operator.

    Because of the above assignment you don't need to try to do the allocation in your constructors.

    Jim

  6. #6
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    Quote Originally Posted by jimblumberg View Post
    That may be true, but you're already allocating memory every time you create an instance of the class.

    And because you manually allocate memory for that pointer every time you create an instance of the class you need to write a destructor, copy constructor and assignment operator, and possibly the move copy consturctor and move assignment operator.

    Because of the above assignment you don't need to try to do the allocation in your constructors.

    Jim
    I did not write destructor and assigment operator this time because I tried to keep things simple for myself. I´m just not familiar with these things jet. What I did was simplified attempt to create something like in this video

    Rule of Three Implementation in C++ - YouTube

    At 9:23 guy in this video creates class with pointer data member and later he creates copy constructor and in that (at 13:20) he creates new pointer to avoid making what he calls "shallow copy". Instead he creates "deep copy".

    Is it not right?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I did not write destructor and assigment operator this time because I tried to keep things simple for myself. I´m just not familiar with these things jet.
    Then perhaps you're not ready to be using pointers in your classes, or perhaps not ready for pointers period.

    At 9:23 guy in this video creates class with pointer data member and later he creates copy constructor and in that (at 13:20) he creates new pointer to avoid making what he calls "shallow copy". Instead he creates "deep copy".
    I'm not interested in watching some video of unknown quality, however that video probably didn't allocate memory for the pointer in the class definition as you did. I really suggest you find and use a good book on C++ programming instead of relying on YouTube.
    Code:
    struct something
    {
        char *some_uninitialized_pointer;
        char *some_initialized_pointer = new char[32];
    With the above code the two pointers would have to be handled differently in your constructor, one would need to have the memory allocated but the other wouldn't. However the destructor should delete the memory for both of those pointers.

    Jim

  8. #8
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    You all were right. Next time I try to be more careful and check things up before posting. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-20-2013, 01:34 PM
  2. Replies: 11
    Last Post: 04-27-2012, 02:40 PM
  3. Copy constructor issue.
    By IndicaHybrid in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2012, 01:47 PM
  4. std container issue (copy semantics?)
    By CodeMonkey in forum C++ Programming
    Replies: 12
    Last Post: 06-29-2007, 07:53 AM
  5. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM

Tags for this Thread