-3

Consider this:

class A {
  public:
    static A test(){
      return A::A();
    }
};

void main(){
  A a = A::test();
}

And it compiles!

UPDATE NO.ONE

Ok, now let's edit this code a bit:

class A {
  public:
    static int test(){
      return A::A();
    }
};

void main(){
  int a = A::test();
}

Does not compile and here is the error

error C2440: 'return' : cannot convert from 'A' to 'int'

I was taught all my life, that a constructor returns nothing! And it seems now, that it actually returns an object. I am completely blown away, please explain me, what is happening here

user3600124
  • 829
  • 1
  • 7
  • 19
  • 4
    Could you clarify why you think this code shows the constructor returns something? – juanchopanza Aug 27 '16 at 12:56
  • 2
    Not the constructor, but static method `MyClass::test` returns an object. – lisyarus Aug 27 '16 at 12:57
  • http://stackoverflow.com/a/17938876/5358284 – polfosol ఠ_ఠ Aug 27 '16 at 12:57
  • Whatever constructor returns, it is successfully converted (is conversion even needed here?) to an object of MyClass ... – user3600124 Aug 27 '16 at 12:57
  • @lisyarus What is that static method doing? – nbro Aug 27 '16 at 12:58
  • Insert this code: `operator int() const {return 0;}` inside your class and it will compile again. This is called conversion operator. What you are trying to do is converting `MyClass` to `int` in your second example. By the way, main does not return `void`, it returns `int` – Amadeus Aug 27 '16 at 13:22
  • Update your IDE, mate. `void main();` will work perfectly well to you after that. And I do not want to convert my object to int. All I want is to understand, what A::A() constructor returns – user3600124 Aug 27 '16 at 13:26
  • @user3600124 You should update yours. [`main` should return `int` (`void` is even explicitely prohibited)](http://stackoverflow.com/a/204483/4538344) and you can't call a constructor directly. – StenSoft Aug 27 '16 at 13:30

2 Answers2

1

return MyClass(); is not a call to a constructor. Firstly, because that method is static and constructors are not, you won't be able to call it without providing an instance. Secondly, you can't call a constructor directly.

MyClass() creates a temporary variable of type MyClass (which implicitly calls a constructor). You can call the same code outside of MyClass.

StenSoft
  • 9,369
  • 25
  • 30
  • Sorry to tell you that, but I have corrected my topic a bit. Made an explicit call to the constructor. Still compiles – user3600124 Aug 27 '16 at 13:24
  • It does on my Visual Studio 2013... not there though. `void main();` doesn't work there either – user3600124 Aug 27 '16 at 13:31
  • @user3600124 If Visual Studio does allow calling constructor directly (which is not allowed by ISO C++), it can also return some non-standard value. – StenSoft Aug 27 '16 at 13:38
0

In C++, a constructor has an evaluation type of the class it resides in and requires no return statement. It's simply called by specifying the type and parameters in parentheses (e.g., MyClass()). That said, many languages have 'named constructors' and this pattern can be implemented in C++ using a static member function that returns a new instance - and thus has an evaluation type of the enclosing class. This is what we observe here: MyClass::test() is a named constructor. It's not strictly a constructor by language grammar, since it has to call another constructor in the implementation; but it's a constructor in the sense that it returns a new instance of the class.

lorro
  • 10,687
  • 23
  • 36