测试t之间的区别是什么?和Test t();?如果Test是一个类 [英] What's the differences between Test t; and Test t();? if Test is a class

查看:161
本文介绍了测试t之间的区别是什么?和Test t();?如果Test是一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么没有调用构造函数?

我使用Visual studio 2012,假设测试是一个类

I am using Visual studio 2012, Suppose Test is a class

class Test
{
};

当我创建一个Test的新实例时,以下两种方法有什么区别?

When I create a new instance of Test, what's the difference of the following two ways?

方式1

Test t;

方式2

Test t();

我在下面的代码中得到了这个问题,最初,我在方法2中定义了A的一个实例,我只有一个错误,因为B不提供默认构造函数,但是当我以方式1定义,我有一个额外的错误。

I got this question in the code below, originally, I defined an instance of A in way 2, I got only one error because B does not provide an default constructor, but when I define it in way 1, I got an extra error.

class B
{
    B(int i){}
};

class A
{
    A(){}
    B b;
};

int main(void) 
{ 
    A a(); // define object a in way 2

    getchar() ; 
    return 0 ; 
} 

如果我定义了一个方式1

if I define a in way 1

A a;

我会收到另一个错误


错误C2248:'A :: A':无法访问在类
中声明的私有成员

error C2248: 'A::A' : cannot access private member declared in class 'A'

所以我想这两种方式之间必须有一些差异。

So I guess there must be some differences between the two ways.

推荐答案

测试t; 定义 Test 类型的 t 变量。

测试t(); 声明一个不带参数并返回<$ c $的函数 t c> Test 。

Test t(); declares a function called t that takes no parameters and returns a Test.

这篇关于测试t之间的区别是什么?和Test t();?如果Test是一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆