c++创建对象 Rectangle rect4()到底加不加括号?

之前在写博客的时候,都没注意过编译器的问题。这次注明:code::blocks,gcc版本:4.9.2;


今天在overflow上面看到一个问答,说新建一个不带参数的对象加括号和不加括号,是有区别的?之前我一直以为是没区别的,就实验了下,代码如下:

#include<iostream>

using namespace std;

class Rectangle
{
private:
    int m_w, m_h;

public:
    Rectangle(){cout << "default" << endl;}
    Rectangle (int w, int h)
    {
        cout << "带参构造函数" <<endl;
    }

};


int main()
{
    cout << "Rectangle *rect1 = new Rectangle(3,4);  //";
    Rectangle *rect1 = new Rectangle(3,4);

    cout << "Rectangle rect2(2,3); //";
    Rectangle rect2(2,3);

    cout << "Rectangle rect3();    //";
    Rectangle rect3();  //这儿没有去调用构造函数类中自己定义的构造函数;据说编译器把它当作了函数来处理;
    void b();   //上一句的代码就类似于执行了这么一个函数声明;
    cout << endl;

    cout << "Rectangle rect4;  //";
    Rectangle rect4;

    cout << "Rectangle *prec = new Rectangle; //";
    Rectangle *prec = new Rectangle;  //加不加括号都行;

    return 0;
}

输出如下:

Rectangle *rect1 = new Rectangle(3,4);  //带参构造函数
Rectangle rect2(2,3); //带参构造函数
Rectangle rect3();    //
Rectangle rect4;  //default
Rectangle *prec = new Rectangle; //default


网上是这么说的,29行的代码,编译器认为是一个 返回值为Rectangle,函数名为rec3的函数声明,那既然认为是函数声明,当然也就不会去调用构造函数了。为什么呢?

——————————因为:
 it is that the standard requires the compiler to interpret anything that might be a function declaration as a function declaration, to prevent ambiguity. 


那么,为什么创建带参数的对象时(例如:rect2(2, 3)),带参数的构造函数就能够正常的使用了呢?按理说,编译器也可以把它解释为一个函数的声明?。。。少侠, 等等!编译器能把它解释为函数的声明吗?你再仔细看看!这括号里面可是数字啊。乍一看又有点像调用,但是别忘了前面还有一个Rectangl呢,所以,不是声明也不是调用,那么,就只能构造了呗。



参考:https://stackoverflow.com/questions/877523/error-request-for-member-in-which-is-of-non-class-type#comment46439487_28946839


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <iostream> #include <string> using namespace std; struct CPoint { int x ; int y ; }; class CRectangle { private: const int id;//常量数据成员 static int total;//静态数据成员 const static string sclass; const static int f=1.0f; CPoint lefttop ; CPoint rightdown ; public: CRectangle( ); CRectangle( CPoint& lt, CPoint& rd ); CPoint GetLefttop() const { return lefttop; } CPoint GetRightdown() const { return rightdown; } void SetLefttop(CPoint & pt) { lefttop=pt; } void SetRightdown(CPoint & pt) { rightdown=pt; } int Getid() const { return id; } static int Gettotal() { return total; } int Area( ) const; int Perimeter( ) const; }; int CRectangle::total=0;//静态数据成员必须在类的外部定义(正好一次)。 const string CRectangle::sclass="CRectangle"; CRectangle::CRectangle( ):id(++total) { lefttop.x=0; lefttop.y=0; rightdown.x=1; rightdown.y=1; } CRectangle::CRectangle( CPoint& lt, CPoint& rd ):id(++total) { lefttop = lt ; rightdown = rd ; } int CRectangle::Area( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return wd * ht ; } int CRectangle::Perimeter( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return 2 * ( wd + ht ) ; } int main() { CPoint lt, rd; cin >> lt.x >> lt.y; cin >> rd.x >> rd.y; CRectangle crt(lt,rd);//调用有参构造函数 CRectangle crt2;//调用默认构造函数 //创建常量对象 const CRectangle crt3(lt,rd); cout<<"当前创建的矩形个数为:"; cout<<CRectangle::Gettotal()<<endl; //返回矩形的左上和右下点 CPoint lt1=crt.GetLefttop(); CPoint lt2=crt.GetRightdown(); //显示矩形的坐标 cout<<crt.Getid()<<"号矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; //修改矩形的左上角点 cout<<"请输入矩形新的左上点坐标:"; cin>> lt.x>>lt.y; crt.SetLefttop(lt); lt1=crt.GetLefttop(); //显示修改后矩形的坐标 cout<<"矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示修改后矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值