0

I notice the former enters the constructor I made even if the constructor has no arguments while the latter only enters the constructor I made only if it requires arguments.

WIN

// ConsoleApplication11.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "string"

using namespace std;


class myRectangle {
    int width;
    public:
        int getWith();
        void setWidth(int newWidth) {width = newWidth;};
        myRectangle (int);
        ~myRectangle ();
};
myRectangle::myRectangle (int w) {
    width = w;
    cout << "myRectangel Constructor\n";
}

myRectangle::~myRectangle() {
    cout << "destructor\n";
}

void runObject();

int _tmain(int argc, _TCHAR* argv[])
{
    runObject();
    int exit; cout << "\n\n"; 
    cin >> exit;
    return 0;
}

void runObject() 
{
    myRectangle mr (5);
}

FAIL // ConsoleApplication11.cpp : Defines the entry point for the console application. //

#include "stdafx.h"
#include "iostream"
#include "string"

using namespace std;


class myRectangle {
    int width;
    public:
        int getWith();
        void setWidth(int newWidth) {width = newWidth;};
        myRectangle ();
        ~myRectangle ();
};
myRectangle::myRectangle () {
    cout << "myRectangel Constructor\n";
}

myRectangle::~myRectangle() {
    cout << "destructor\n";
}

void runObject();

int _tmain(int argc, _TCHAR* argv[])
{
    runObject();
    int exit; cout << "\n\n"; 
    cin >> exit;
    return 0;
}

void runObject() 
{
    myRectangle mr ();
}
user1873073
  • 3,580
  • 5
  • 46
  • 81
  • You may want to sit down. `MyRectangle mr2();` is a [function declaration](http://stackoverflow.com/questions/3810570/why-is-there-no-call-to-the-constructor). – jrok May 07 '13 at 16:53
  • Check this. http://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work – stardust May 07 '13 at 16:55

3 Answers3

3
myRectangle mr(5);

Here, mr is a myRectangle instance, constructed using the myRectangle constructor that takes a single int parameter.

myRectangle mr ();

Here, mr is a function with no parameters and returning a myRectangle. It is a confusing parse that can be avoided in C++11 by using brace initialization. It can also be avoided by omitting the brackets:

myRectangle mr; //  C++03 and C++11
myRectangle{};  //  C++11
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

Inside:

 void runObject() 
 {
     myRectangle mr ();
 }

myRectangle mr(); is not creating an object of myRectangle, but rather, declares a function with name mr that takes no parameter and return type is myRectangle.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • For you, maybe, but not for the compiler. To instantiate an object without using constructor parameters, omit the parenthesis: `myRectangle me;` – Remy Lebeau May 07 '13 at 17:13
1

myRectangle mr = myRectangle(); instantiates and constructs an instance of the class myRectangle. In contrast, myRectangle mr (); declares mr as a function returning myRectangle and taking no arguments.