Need help in understanding below code

Discussion in 'C++' started by jagan, Jun 18, 2012.

  1. jagan

    jagan Guest

    Hi All,
    I am trying to learn c++. I studied that construct does not
    return anything and can be used
    to intialise only objects data memebers. Could someone explain how
    following is code is compiling
    in that case.

    #include <iostream>
    using namespace std;

    class complx {
    double re, im;

    // copy constructor
    complx(const complx& c) { re =; im =; }

    // constructor with default trailing argument
    complx( double r, double i = 0.0) { re = r; im = i; }


    int main() {

    complx three = complx(3,4); // Here intialising object by calling
    constructor. How three is getting intialised
    // If constructor is not

    jagan, Jun 18, 2012
    1. Advertisements

  2. The usual way of declaring and initializing a variable in C++ is not:
    complx three = complx(3,4);
    but rather:
    complx three(3,4); // this declares a variable 'three' and
    // calls the constructor on it

    complx(3,4) // this declares an anonymous temporary variable of
    // type 'complex' and calls the constructor on it

    The word 'complex' in all those expressions refers to the type 'complex'
    and not the name of the constructor. (The name of the constructor is
    actually 'complx::complx')
    The constructor is always called implicitly. The right constructor is
    selected by looking at the arguments.

    Additionally, in C++ the following two statements are considered the same:
    complx three = complx(3,4);
    complx three(complx(3,4));
    So, what you do is actually the following:
    - declare an anonymous temporary variable of type 'complx'.
    - this variable is initialized by calling the constructor
    complx::complx(double r, double i)
    - declare a variable called 'three' of type 'complx'
    - initialize the variable by calling the copy constructor with the
    anonymous variable from before.

    I hope this helps
    Tobias Müller, Jun 18, 2012
    1. Advertisements

  3. Not "construct", but "constructor". It's a special function that is
    called by the program every time an object of that type is created.
    It's not entirely correct to say that it doesn't return anything. It
    has no specified return value type, yes. It is more proper to say that
    a constructor returns the new object (although it doesn't need to have a
    Well, if the constructor is defined for the class, it's *the only* place
    where data members are initialized (or not).
    In the following code what you describe as "initializing an object by
    calling constructor", is in fact initializing an object *from another,
    temporary object". The expression 'complx(3,4)' creates a temporary
    object (without giving it a name), and then uses that object to
    initialize your named object 'three'.

    The object 'three' is initialized using the copy constructor, from the
    temporary. It's *almost the same* as writing

    complx three(3,4);

    but not quite.

    What book are you reading to learn C++? How carefully did you study the
    constructors and initialization chapter? Perhaps you need to read a
    better book, or at least go back to the one you have already read...

    Also, see section 10 of the FAQ.
    Victor Bazarov, Jun 18, 2012
  4. jagan

    Old Wolf Guest

    What this syntax actually does is:
    complx three( complx(3,4) );

    In English, it constructs a temporary complx(3,4),
    and then uses that to copy-construct "three". Your
    compiler might optimize this process.

    The "=" sign in a declaration has a different
    meaning to the "=" in an assignment.
    Old Wolf, Jul 9, 2012
  5. These two data members are implicitly private, in case you didn't know.
    This allows me to do this:
    complx a(3.2, 2.9); //creates a new complx object named a
    complx b(a); //calls copy constructor of b
    `b`'s default constructor created the two members `re` and `im`. Then the copy constructor assigned to `re` and `im` the values of those in `a`.
    This simply initializes the private data members to the values you pass to it. `i` has a default value, which means you can use `complx` this way:
    complx c(3.2);
    And `` would have the value of 0.0.
    richardturdman, Jul 9, 2012
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.