显式默认构造函数 [英] explicit default constructor

查看:97
本文介绍了显式默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


在实践中,默认构造函数和

显式默认构造函数之间有什么不同?

class Ai

{

public:

Ai(){}

};


class Ae

{

public:

显式Ai(){}

};


另一个问题。我在另一个帖子中读到,如果你明确地提供一个构造函数,那么实现不应该隐式地表示

声明/定义默认构造函数和复制构造函数

(12.1),但我无法找到标准所说的规则。是吗?b $ b真的如此吗?它在哪里说明了?


谢谢,

Marcelo Pinto。

Hi all,

In practice, what is the diference between a default constructor and
an explicit default constructor?

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
};

Another question. I read in another thread that, if you provide
explicitly one constructor, the implementation shall not implicitly
declare/define the default constructor and the copy constructor
(12.1), but I couldn''t find where the standard stated this rule. Is it
really true? Where is it stated?

Thanks,

Marcelo Pinto.

推荐答案

Marcelo Pinto写道:
Marcelo Pinto wrote:
在实践中,默认构造函数和显式默认构造函数之间的区别是什么?


无,IIUIC。

类Ai
{
公开:
Ai(){}
};

类Ae
{
公开:
显式Ai(){}


显式Ae(){ }

};

另一个问题。我在另一个帖子中读到,如果你明确地提供了一个构造函数,那么实现不应该隐式地声明/定义默认构造函数和复制构造函数
(12.1),但我不能''找到标准所述的规则。它真的是真的吗?在哪里说明了?
In practice, what is the diference between a default constructor and
an explicit default constructor?
None, IIUIC.

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
explicit Ae() {}
};

Another question. I read in another thread that, if you provide
explicitly one constructor, the implementation shall not implicitly
declare/define the default constructor and the copy constructor
(12.1), but I couldn''t find where the standard stated this rule. Is it
really true? Where is it stated?




不,这不是真的。至少它并不完全正确。如果你
声明一个参数化的c-tor,则没有默认的c-tor隐含声明

。仍然会提供隐式复制c-tor。


参见Standard,12.1 / 5如果没有用户声明的构造函数

类X,默认构造函数是隐式声明的。


Victor



No, it''s not really true. At least it''s not completely true. If you
declare a parameterized c-tor, there will be no default c-tor declared
implicitly. The implicit copy c-tor is still going to be provided.

See Standard, 12.1/5 "If there is no user-declared constructor for
class X, a default constructor is implicitly declared."

Victor


Marcelo Pinto写道:
Marcelo Pinto wrote:
大家好,

在实践中,默认构造函数和显式默认构造函数之间有什么区别?

类Ai
{
公众:
艾(){}
};

类Ae
{
公开:
显式Ai(){}
};


如果你将构造函数标记为显式,那么编译器就无法使用那个构造函数来使用它。调整类型。目前

我想不出一个人想用默认的

构造函数来做这个,不需要参数。

但是


A级
{

公开:

A(int i_ = 0):i( i_){}

受保护:

int i;

};


void foo( A Arg)

{

}


int main()

{

foo(5);

}


这里编译器使用构造函数创建一个对象

可以用来调用foo。如果您将构造函数标记为显式,

则不再允许这样做。你需要写一下


int main()

{

foo(A(5));

}


因此明确地创建A对象。


另一个问题。我在另一个帖子中读到,如果你明确地提供了一个构造函数,那么实现不应该隐式地声明/定义默认构造函数和复制构造函数
(12.1),但我不能''找到标准所述的规则。它真的是真的吗?


No.


如果你没有声明任何构造函数,编译器会声明

a默认构造函数。

如果你没有声明一个复制构造函数,编译器也会声明

a复制构造函数。


所以:

声明的任何构造函数(复制构造函数除外)

- >编译器不会声明默认构造函数


您声明了一个复制构造函数 - >编译器将不再为你声明

a复制构造函数。

它在哪里声明?

Hi all,

In practice, what is the diference between a default constructor and
an explicit default constructor?

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
};

If you mark a constructor as explicit, then the compiler cannot
use that constructor under the hood to adjust types. At the moment
I cannot think of a case where one wants to do this with a default
constructor that takes no arguments.
But

class A
{
public:
A( int i_ = 0 ) : i( i_ ) {}
protected:
int i;
};

void foo( A Arg )
{
}

int main()
{
foo( 5 );
}

Here the compiler uses the constructor to create an A object which
can be used to call foo. If you mark the constructor as explicit,
then this is no longer allowed. You would need to write

int main()
{
foo( A(5) );
}

and thus create the A object explicitely.

Another question. I read in another thread that, if you provide
explicitly one constructor, the implementation shall not implicitly
declare/define the default constructor and the copy constructor
(12.1), but I couldn''t find where the standard stated this rule. Is it
really true?
No.

If you don''t declare any constructor, the compiler will declare
a default constructor.
If you don''t declare a copy constructor, the compiler will declare
a copy constructor too.

So:
any constructor declared (with the exception of the copy constructor)
-> the compiler will not declare a default constructor

you declare a copy constructor -> the compiler will no longer declare
a copy constructor for you.
Where is it stated?




第12节12.1很好


-

Karl Heinz Buchegger
kb ****** @ gascad.at




" Marcelo Pinto" <熔点**** @ brturbo.com>在消息中写道

news:7a ************************** @ posting.google.c om ...

"Marcelo Pinto" <mp****@brturbo.com> wrote in message
news:7a**************************@posting.google.c om...
大家好,

在实践中,默认构造函数和显式默认构造函数之间有什么区别?

类Ai
{
公开:
艾(){}
};

类Ae
{
公开:显式Ai(){}
};


我不认为第二个是合法的。显式是对于可以用一个参数调用的构造函数,它可以防止构造函数被用于

隐式地将参数的类型转换为类的类型。


struct A

{

A();

A(int);

};


struct B

{

B();

显式B(int);

};


A a;

B b;

a = 2; //合法

b = 2; //非法构造函数是明确的

b = B(2); //合法,构造函数是明确使用的

另一个问题。我在另一个帖子中读到,如果你明确地提供了一个构造函数,那么实现不应该隐式地声明/定义默认构造函数和复制构造函数
(12.1),但我不能''找到标准所述的规则。它真的是真的吗?它在哪里陈述?
Hi all,

In practice, what is the diference between a default constructor and
an explicit default constructor?

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
};
I don''t think the second is legal. Explicit is for constructors that can be
called with one argument and it prevents that constructor from being used to
implicitly convert the type of the argument to the type of the class.

struct A
{
A();
A(int);
};

struct B
{
B();
explicit B(int);
};

A a;
B b;

a = 2; // legal
b = 2; // illegal constructor is explicit
b = B(2); // legal, constructor is used explicitly

Another question. I read in another thread that, if you provide
explicitly one constructor, the implementation shall not implicitly
declare/define the default constructor and the copy constructor
(12.1), but I couldn''t find where the standard stated this rule. Is it
really true? Where is it stated?




对于默认构造函数来说,对于副本

构造函数来说不是这样。这与你上面提到的
的显式关键字无关。


john



Its true for the default constructor, it is not true for the copy
constructor. This have nothing to do with the explicit keyword that you
mentioned above.

john


这篇关于显式默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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