Thread: Copy constructor question

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    39

    Copy constructor question

    Suppose I have a class like this:
    Code:
    class aClass {
      char *ptr; // random variable, not important
      int time;
      aClass *bClass[5];
    public:
      aClass();
      ~aClass();
      aClass(const aClass& cpy); // copy constructor
    };
    So bClass is like a pointer to an array of "sub classes" (within aClass). Let's say in my program I initialize and have bClass[0] and bClass[1], and I want to make a class bClass[3] that is a copy of bClass[1]. I tried something like this (does not work):
    Code:
    aClass bClass[3](bClass[1]);
    I've also looked through my textbook and online and saw that I can overload the assignment operator which will act similarly to the copy constructor, but I want to know how to do this with a copy constructor? The actual code is different of course but this is just a simplified view of what I want to do. Thanks!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    aClass *bClass[5];

    So bClass is like a pointer to an array
    No - it is array of pointers...
    I want to make a class bClass[3]
    this is the third element of the array? it is initialized with default constructor when you created the array, you cannot reinitialize it using copy-constructor...

    assignment operator is seems to be a way to go here
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    39
    Well bClass[] is actually dynamically allocated with
    Code:
    bClass[i] = new aClass(arg1, arg2);
    looping through a for loop in a separate function (arg1 and arg2 are arguments to the constructor for initializing data members). Let's say 5 is the maximum the array can hold, so if I only dynamically allocate for bClass[0] and bClass[1], then bClass[2], etc. will still be not allocated. Is it possible to use a copy constructor and dynamically allocate for bClass[3] to be a copy of bClass[1] or bClass[2]? Please tell me if anything is unclear, I'll try my best to explain. I just want to know if it's possible to do with a copy constructor and how? thanks for your help again

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    bClass[3] = new aClass(bClass[1]);
    should work
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    If you use vector and shared_ptr, you won't need to worry about copy constructors and assignment operators. In fact, you should probably make a design decision of whether or not you need copy semantics at all. I find that the overwhelming majority of the time, I don't.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    39
    vart, that works! thanks. Just one more question about copy constructors in general: You can specify additional parameters for a copy constructor (in addition to the reference to same class type) as long as you specify a default value right? What If I want to pass a char* to the copy constructor? How do I specify a default value for that?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You can specify additional parameters for a copy constructor
    it will not be copy constructor - just another constructor with additional parameters

    How do I specify a default value for that?
    What is so different here from regular default value specification?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    it will not be copy constructor - just another constructor with additional parameters
    No, BigFish21 is correct. It will still be a copy constructor, since the additional parameters all have defaults.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And you can't (well, shouldn't) specify a default for a char*. You can simply specify a string literal as the default for a const char*.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    And you can't (well, shouldn't) specify a default for a char*. You can simply specify a string literal as the default for a const char*.
    You can always specify NULL as the default, meaning "I don't care about that parameter and I'm not using it". Of course you'd obviously need logic in the function to handle the NULL case properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gcc can't find obvious copy constructor
    By SevenThunders in forum C++ Programming
    Replies: 13
    Last Post: 03-19-2009, 02:41 PM
  2. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 04:54 PM
  3. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  4. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM