Thread: Copy constructor

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Copy constructor

    Hi Guys,

    I have a structure and for this i want to create a copy constructor.

    Code:
    struct my_Export exportData
    {
    int a;
    char b[10];
    short c;
    //there are lots more variable all of int,char data types;
    exportData *dataPtr;
    };
    For other data members it is easy to copy by assignment. But for dataPtr i want deep copy to occur.
    S_ccess is waiting for u. Go Ahead, put u there.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, let me question if you really need a pointer, and if you really need a char array. Is it possible to get get rid of them? If so, do it. Replace the char array with std::string. Replace the pointer with a non-pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    First, let me question if you really need a pointer,
    It is pointing to the same type as struct itself... Looks like some chaining of structs here.

    But maybe they could be stored in the std::queue instead...
    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

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    pointer is the base requirement, I can not change this. And that is why i am looking the ways to write copy constructor for this.
    S_ccess is waiting for u. Go Ahead, put u there.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then what part is it that you do not understand?
    The syntax for writing a copy constructor?
    How to deep copy a pointer?
    Are you allowed to use smart pointers (requires C++11/TR1)? If so, that might also be a viable solution which requires less code, and no delete.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    syntax i am aware of:
    Code:
    exportData::exportData(const exportData& d):a(d.a),c(d.c){
    strcpy(b, d.b);
    dataPtr = new dataPtr();
    //After this how to make everything copy again in dataPtr, i was thinking of call like dataPtr(d.dataPtr), but it will loop in copy constructor!!
    }
    Can not use autoptr.
    S_ccess is waiting for u. Go Ahead, put u there.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by maven View Post
    Can not use autoptr.
    Not auto_ptr. std::unique_ptr/std::shared_ptr.
    Anyway, to answer your question, here's an example of something:

    Code:
    int x = 0;
    int * y = new int(5);
    int * z = new int (10);
    std::cout << x << " " << *y << " " << *z << "\n";
    x = *y;
    *z = x;
    *y = *z;
    std::cout << x << " " << *y << " " << *z << "\n";
    delete y;
    delete z;
    Now remember that you can generalize this to any type, not just int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    What is the context of these x and y. Are you considering these structure member variables? I am not able to relate my question with your answer!!

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They are local variables - but the principle is the same. Local, global, member - doesn't matter.
    The example shows how to copy values between non-pointers and pointers and pointer and pointers.
    Swap out int for any type - if the type is copyable (hint: most types are), then it will work just fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Considering example i used. If i copy pointer like this then it is a shallow copy. whereas i asked how to do deep copy here.
    Code:
    exportData::exportData(const exportData& d):a(d.a),c(d.c){
    strcpy(b, d.b);
    dataPtr = d.dataPtr;
    }
    now considering "d.dataPtr" as a non pointer and dataPtr as pointer, have you shared above example to do deep copy?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it does. Consider a moment: what, exactly, does a deep copy do? What does it mean?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    In a deep copy instead of sharing same address space we use different address space for variables(pointer).
    And you mean "dataPtr = d.dataPtr;" is doing deep copy!

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We copy the data instead of the address.
    Then consider these alternatives:

    int* x = new int;
    int* y = new int;
    int a, b;
    a = b;
    b = x;
    x = y;
    *y = *x;
    delete x;
    delete y;

    Which rows are compile errors, shallow copies and deep copies?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Error will be at:
    Code:
    b=x;
    int=int*;
    S_ccess is waiting for u. Go Ahead, put u there.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Right. So which are shallow copies, and which are deep copies?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC and Copy Constructor
    By NA9x2000 in forum C++ Programming
    Replies: 7
    Last Post: 08-25-2012, 09:38 AM
  2. Copy constructor
    By dude543 in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2006, 05:35 PM
  3. what is copy constructor?
    By Tonyukuk in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2002, 05:54 PM
  4. Copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 02:02 PM
  5. copy constructor
    By ygfperson in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2002, 06:55 PM