gpt4 book ai didi

C++用括号声明一个对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:09 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Error on calling default constructor with empty set of brackets

我附上了一个测试程序。问题:

如果我像下面这样声明,就没有对象被创建并且不调用默认构造函数。 'grCell c3();'//不好

但是,像这样声明是可以的。创建一个对象并调用其构造函数。 'grCell c1;'//很好

'grCell c3()' 和 'grCell c1' 有什么区别?

谢谢!

托德

//----开始--------

#include <iostream>
#include <cstdio>

typedef unsigned int uint;
using namespace std;

//
class grCell {
public:
grCell() { printf("HERE_0\n"); };
grCell(int i) { printf("HERE_1\n"); };
~grCell() {};

void setX(int x) { _x = x; }
//
//
private:


int _x:22;
};

int main()
{

grCell c1; // good
c1.setX(100);


grCell c3(); // bad
c3.setX(100);

grCell c2(5);
c2.setX(10);


}

//------ 结束 ------

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com