1

There are many ways to initialize an object, one of which is calling a constructor of centain user-defined type. Here are the examples.

Hello my_hello = Hello(3);
Hello my_hello(3);

As you already know, a constructor doesn't return anything. However, as you look above, it seems that it does return its initialized object. Furthermore, the assignment operator makes it more suspicious because it means it copys the right-hand object to the left-hand object.

Am i guessing wrong? Would you please explain it?

  • 3
    Have you learned copy constructor? – billz Aug 10 '13 at 11:54
  • Sometimes thoughts out of ignorance are so bewildering that it starts feeling the work of acute intelligence. – Nawaz Aug 10 '13 at 11:57
  • 1
    The former *does not* use assignment operator. It's called copy initialization while the latter is called direct initialization. Assignment operator can only be used on already constructed objects. – jrok Aug 10 '13 at 11:59
  • constructor constructing object – RiaD Aug 10 '13 at 12:00
  • Related: http://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initializati – jrok Aug 10 '13 at 12:02

3 Answers3

3

The declaration

Hello my_hello = Hello(3);

creates two objects. First a temporary object from Hello(3), and this temporary object is then copied through the copy-constructor to my_hello followed by the destruction of the temporary object.

However, to further muddle up the water, this copying and destruction of the temporary object may actually not happen due to copy elision, a very common compiler optimization.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Just calling a constructor without an instantiation yields a temporary object initialized by the constructor? –  Aug 10 '13 at 12:35
  • Thank you for such a kind explanation. By the way, i have an another question. What about this? int a = int(3); It is obvious that the int is not user-defined type. Therefore, int(3) is not calling a constructor. It's so complicated. :-( –  Aug 10 '13 at 12:44
  • @isbae93 It's just the same, it creates a temporary `int` with the value `3` and copies that to `a`. – Some programmer dude Aug 10 '13 at 12:46
1

As you already know, a constructor doesn't return anything.

This is strictly true, per Standard.

However, it's an awful lot simpler to consider a constructor for T as a function returning a T (or emplacing it, a'la new and placement new). This is essentially how they're both used and implemented.

Also, there is no assignment operator in your code. That is copy-initialization, which is not the same thing. The my_hello object is copied from Hello(3).

Puppy
  • 144,682
  • 38
  • 256
  • 465
1

only one way to find out...try some code :

#include <iostream>
#include <cstdio>

using namespace std;

class Hello{
    public:
    Hello(int n)
    {
        printf("CTOR\n");
    }

    Hello& operator=(Hello& h)
    {
        printf("assignment\n");
        return *this;
    }
};

int main() {

    printf("first:\n");
    Hello firstHello(3);

    printf("\nsecond:\n");
    Hello secondHello = Hello(4);

    return 0;
}

result :

first:
CTOR

second:
CTOR

i.e. no assignment operator called. should really add a copy constructor and check that is used instead...consider it an exercise for the reader.

http://ideone.com/9Yr2WY

EDIT : out of curiosity, here is a version including a copy constructor. In this case neither of the lines calls the copy constructor - as Joachim points out, the compiler has succeeded in optimizing out the copy : http://ideone.com/wQ1VTK

Graham Griffiths
  • 2,196
  • 1
  • 12
  • 15