Copy constructor question

This is a discussion on Copy constructor question within the C++ Programming forums, part of the General Programming Boards category; Suppose I have a class like this: Code: class aClass { char *ptr; // random variable, not important int time; ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    bClass[3] = new aClass(bClass[1]);
    should work
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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, 03: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, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21