Thread: Constructors and assignment

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    Constructors and assignment

    Hello, everyone. I'm learning C++ after coming from a C background. C++ does my head in by complicating things that in C can be assumed to be simple! Could someone please clarify for me *exactly* what operations are generated by the following lines of code - constructors, copying, assignment, etc?

    Code:
    1.  int i = 0;
    2.  int i ; i = 0;
    3.  int i(0);
    4.  int i {0};
    Am I able to overload all of those operations for int? Is there any difference between "int" and a user-defined type in terms of which operations I am able to overload?

    Richard

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,445
    What do you think are the operations generated? And why?

    What do you mean by overload?

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    144
    Hi, Jim. This is what I think are the operations generated.
    Code:
    1.  int i = 0;
        int j(0);  // int constructor, given a parameter of 0
        int i(j);  // int copy constructor, given a reference to j
    Code:
    2.  int i ; i = 0;
        int i(); // int constructor, given no parameter
        int j(0); // int constructor, given a parameter of 0
        i = j; // assignment operator
    Code:
    3.  int i(0);
        // Calls int constructor with parameter of 0
    Code:
    4.  int i {0};
        // Calls int constructor with parameter of 0
    The second part of my question is - is there any difference between builtin types and user-defined types in terms of my ability to define/overload constructors and operators?

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Yes! We can most certainly help you, OP.

    Consider using Compiler Explorer

    Here you can play around with all kinds of C++ code and see how it compiles across various compilers.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by Richardcavell View Post
    Am I able to overload all of those operations for int? Is there any difference between "int" and a user-defined type in terms of which operations I am able to overload?
    You can't overload anything at all for built in types. I.e., you can't change the meanings of the operators for built in types, but you can define meanings for them when used with user-defined types.

    As for the code generated by your examples, they would all be identical. In what way could they differ? They allocate space for an integer and set it to 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy Constructors and Assignment Operator overloading.
    By leeor_net in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2009, 10:26 PM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  4. Constructors
    By incognito in forum C++ Programming
    Replies: 6
    Last Post: 12-30-2001, 10:55 PM
  5. Replies: 2
    Last Post: 12-17-2001, 06:40 PM

Tags for this Thread