copy constructors and the assignment operator

Discussion in 'C++' started by John Ratliff, Aug 10, 2005.

  1. John Ratliff

    John Ratliff Guest

    What are the ramifications of creating a private copy constructor?

    Say I didn't want any copies to be created, say if I had a singleton for
    example.

    Should I make the copy constructor public or private? I don't plan on
    defining it. Does it make a difference?

    If I make it private, what happens to the assignment operator? Does it
    try to use the undefined private copy constructor, or would it use the
    default copy constructor?

    If the undefined copy constructor were public, what happens to the
    assignment operator? Will it try to use the undefined public copy
    constructor or will it use the default? I would think in this case it
    would have to try and use the public undefined copy constructor, but
    then fail since it's undefined.

    Thanks,

    --John Ratliff
     
    John Ratliff, Aug 10, 2005
    #1
    1. Advertising

  2. John Ratliff wrote:
    >
    > What are the ramifications of creating a private copy constructor?
    >


    As with any other private thing:
    Access is granted to member functions of that class only.
    Every other code that is not a member function of that class doesn't have
    access to that part of the class (the copy constructor).

    > Say I didn't want any copies to be created, say if I had a singleton for
    > example.
    >
    > Should I make the copy constructor public or private? I don't plan on
    > defining it. Does it make a difference?


    Sure.
    If you make it public, then any other code can access that function ->
    The compiler will happily compile that other code.
    Only during the linking stage, the linker will notice that there is no
    implementation for that function and will barf.

    If you make it private, then no other code has the access rights to
    use that constructor. -> The compiler will emit an error message
    That leaves you with the special case of using the copy constructor
    from one of the member functions of the very same class. They have
    access rights and the compiler will happily encode calls to the copy
    constructor in them. But again: Eventually the linking stage starts
    and the linker will notice that the function is not implemented.

    >
    > If I make it private, what happens to the assignment operator?


    Nothing at all.

    > Does it
    > try to use the undefined private copy constructor, or would it use the
    > default copy constructor?


    ????
    Assignment operator and constructors have nothing to do with each other.
    Why do you think that op= uses a constructor for anything?

    A constructor is used for creating objects.
    When an op= runs, then the object has been created long before.


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 10, 2005
    #2
    1. Advertising

  3. John Ratliff

    John Ratliff Guest

    >>Does it
    >>try to use the undefined private copy constructor, or would it use the
    >>default copy constructor?

    >
    >
    > ????
    > Assignment operator and constructors have nothing to do with each other.
    > Why do you think that op= uses a constructor for anything?
    >
    > A constructor is used for creating objects.
    > When an op= runs, then the object has been created long before.
    >
    >


    Sorry, I was under the impression that the default assignment operator
    used the default copy constructor.

    Thanks,

    --John Ratliff
     
    John Ratliff, Aug 10, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Ford
    Replies:
    1
    Views:
    5,325
    Ron Natalie
    Nov 20, 2003
  2. Jeremy Smith
    Replies:
    2
    Views:
    612
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    626
    Ron Natalie
    Jun 7, 2007
  4. Weihui Shen
    Replies:
    5
    Views:
    408
    James Kanze
    Aug 3, 2007
  5. srp113
    Replies:
    3
    Views:
    489
Loading...

Share This Page