copy constructor

This is a discussion on copy constructor within the C++ Programming forums, part of the General Programming Boards category; is a copy constructor needed? i have a class, bitstream, which is called this way: Code: main() { bitstream streamer("the"); ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    copy constructor

    is a copy constructor needed?
    i have a class, bitstream, which is called this way:
    Code:
    main() {
    bitstream streamer("the");
    dummy(streamer);
    return 0; }
    
    int dummy(bitstream x) {}
    This code results in a crash. i'm thinking maybe i need a copy constructor. am i right?

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    from examining what you included it doesn't show that you need anything.....

    it would be better if you would post all the the code...

    Regards,
    matheo917

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i can't post the whole thing, because part of it's an entry to the flashdaddee contest, but here's the important stuff...
    Code:
    #include <cstdio>
    class bitstream {
    private:
    int length;
    int bit_length;
    unsigned char bitt[65536];
    public:
      bitstream (char *temp) {
       //blah... temp doesn't get changed
      }
      int print() { }
    };
    
    int dummy(bitstream); //other problem area
    int bit(int x,char y) {}  //gets the value of a bit in a char, works fine
    int dummy(bitstream x) {} //causes crash
    
    int main()
    {
    char input[128];
    int x=0;
    fgets(input,128,stdin);
    bitstream streamer(input); //works fine
    dummy(streamer);  //causes error
    streamer.print(); //to print results
    return 0;
    }
    when compiled, this piece of code crashes.
    Last edited by ygfperson; 03-05-2002 at 04:16 PM.

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    from what i am looking at i can't spot a problem......

    hint: i see u r using pointers..... if inside of one of those (not included) functions you allocate memory dynamically...or simply said you add, or assign one value to another of objects that are on the heap you need to define your own copy constructor, as well as overload (most likely) the assigment operator....

    anyway....you should of just posted the code inside of the function that caused the problem....

    well, so far i don't see it, maybe someone else will...

    good luck....

    Regards,
    matheo917

  5. #5
    Unregistered
    Guest
    when I was studying I was told a copy constructor was part of the canonical class form (default constructor, copy constructor, overloaded = operator and a destructor). The reason for this is that people will be able to use the basic operations they might expect. Therefore I would try a copy constuctor and see if it works, they're not too hard to write.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    the code there isn't the whole code, but when only that snippet's cut and pasted, it crashes like before. it can't be anything i'm not showing.

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    nm, fixed it. it was the copy constructor. when i added the line
    Code:
    bitstream(bitstream& p) { }
    it stopped crashing. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructor
    By dude543 in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2006, 04:35 PM
  2. illegal copy constructor required?
    By ichijoji in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2005, 05:27 PM
  3. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM
  4. copy constructor
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2002, 05:03 PM
  5. Using strings with the copy constructor
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 03:04 PM

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