初始化函数参数 [英] initialization of function argument

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

问题描述

假设我们有一个名为Test的类。


测试obj; //假设默认ctor可用

测试direct_init(obj); //直接初始化发生在这里

测试copy_init = obj; //复制初始化发生在这里


假设我们有一个函数


void fn(Test arg);


当我们用fn(obj)调用fn()时,调用复制程序

来构造''arg''。我是否正确?

问题是什么样的初始化 - 直接

初始化或复制初始化,碰巧

构建arg?

标准在这方面说了什么?

如果复制初始化发生,那么如果副本

ctor是''explicit'',那么调用fn( obj)不会b
编译。我是对的吗?


请说明


谢谢

V.Subramanian

Suppose we have a class named Test.

Test obj; // assuming default ctor is available
Test direct_init(obj); // direct initialization happens here
Test copy_init = obj; // copy initialization happens here

Suppose we have a function

void fn(Test arg);

When we call fn() with fn(obj), the copy ctor
is called to construct ''arg''. Am I correct?
Question is what kind of initialization - direct
initialization or copy initialization, happens to
construct arg ?
What does the standard say in this regard ?
If copy initialization happens, then if the copy
ctor is ''explicit'', then calling fn(obj) will not
compile. Am I correct ?

Kindly explain

Thanks
V.Subramanian

推荐答案



< su ************** @ yahoo.comwrote in message

news:84 ********************************** @ e23g2000 prf.googlegroups.com ...

<su**************@yahoo.comwrote in message
news:84**********************************@e23g2000 prf.googlegroups.com...

假设我们有一个名为Test的类。


测试obj; //假设默认ctor可用

测试direct_init(obj); //直接初始化发生在这里
Suppose we have a class named Test.

Test obj; // assuming default ctor is available
Test direct_init(obj); // direct initialization happens here



我不确定你的''直接初始化'是什么意思,但是

以上line调用复制构造函数。

I''m not sure what you mean by ''direct initialization'', but
the above line invokes the copy constructor.


Test copy_init = obj; //复制初始化发生在这里
Test copy_init = obj; // copy initialization happens here



这也将调用复制构造函数。

This will also invoke the copy constructor.


>

假设我们有一个函数


void fn(测试arg);


当我们用fn调用fn()时(obj),复制ctor

被调用来构造''arg''。我对么?
>
Suppose we have a function

void fn(Test arg);

When we call fn() with fn(obj), the copy ctor
is called to construct ''arg''. Am I correct?



是。

Yes.


问题是什么样的初始化 - 直接

初始化或复制初始化,恰好是
构造arg?
Question is what kind of initialization - direct
initialization or copy initialization, happens to
construct arg ?



复制构造。

Copy construction.


标准在这方面说了什么?
What does the standard say in this regard ?



我手边没有我的副本。

I don''t have my copy at hand.


如果发生了复制初始化,那么如果副本

ctor是''显式'',然后调用fn(obj)将不会b / b
编译。
If copy initialization happens, then if the copy
ctor is ''explicit'', then calling fn(obj) will not
compile.



当然可以。

Sure it will.


我是否正确?
Am I correct ?



No.


但是,如果复制构造函数被声明为明确的,

该行


测试copy_init = obj;


将无法编译。 (VC ++ 2005说:

class''Test'':没有可用的复制构造函数或

复制构造函数被声明为''explicit''

No.

However, if the copy constructor is declared explicit,
the line

Test copy_init = obj;

will not compile. (VC++ 2005 says:
class ''Test'' : no copy constructor available or
copy constructor is declared ''explicit''


请解释
Kindly explain



运行下面的示例。

#include< iostream>


班级考试

{

static int i;


public:

测试()

{

++ i;

std :: cout<<" Obj" ;<<<<<""默认构造函数\ n" ;;

}


测试(const测试& t)
{

++ i;

std :: cout<<"" Obj"<<<<<<"复制构造函数\ n" ;;

}


测试&运算符=(const测试& t)

{

++ i;

std :: cout<< std :: cout<<"" Obj"<<< i<<"" Assignment operator \ n" ;;

返回* this;

}


~测试()


{

std :: cout<< Obj << i<< " destructor \ n" ;;

- i;

}

};


int测试:: i;


void fn(测试arg)

{

}


int main()

{

测试对象;

测试direct_init(obj);

测试copy_init = obj;

std :: cout<< 在调用fn \ n之前;

fn(obj);

std :: cout<< 在fn()\ n之后;

返回0;

}

输出:


Obj 1默认构造函数

Obj 2复制构造函数

Obj 3复制构造函数

调用fn之前

Obj 4复制构造函数

Obj 4析构函数

后fn()

Obj 3析构函数

Obj 2析构函数

Obj 1析构函数


-Mike

Run my example below.
#include <iostream>

class Test
{
static int i;

public:
Test()
{
++i;
std::cout << "Obj " << i << " Default constructor\n";
}

Test(const Test& t)
{
++i;
std::cout << "Obj " << i << " Copy constructor\n";
}

Test& operator=(const Test& t)
{
++i;
std::cout << std::cout << "Obj " << i << " Assignment operator\n";
return *this;
}

~Test()

{
std::cout << "Obj " << i << " destructor\n";
--i;
}
};

int Test::i;

void fn(Test arg)
{
}

int main()
{
Test obj;
Test direct_init(obj);
Test copy_init = obj;
std::cout << "Before calling fn\n";
fn(obj);
std::cout << "After fn()\n";
return 0;
}
Output:

Obj 1 Default constructor
Obj 2 Copy constructor
Obj 3 Copy constructor
Before calling fn
Obj 4 Copy constructor
Obj 4 destructor
After fn()
Obj 3 destructor
Obj 2 destructor
Obj 1 destructor

-Mike


2007-11-25 17:21,Alf P. Steinbach写道:
On 2007-11-25 17:21, Alf P. Steinbach wrote:

* Mike Wahler:
* Mike Wahler:

>< su **** **********@yahoo.com写了留言
><su**************@yahoo.comwrote in message

>>>
void fn(Test arg); <当我们用fn(obj)调用fn()时,调用copy复制器来构造''arg''。我对么?
>>>
void fn(Test arg);

When we call fn() with fn(obj), the copy ctor
is called to construct ''arg''. Am I correct?


是的。


Yes.


>>问题是什么样的初始化 - 直接
初始化或复制初始化,碰巧构建arg?
>>Question is what kind of initialization - direct
initialization or copy initialization, happens to
construct arg ?


复制结构。


Copy construction.


>>标准在这方面说了什么?
>>What does the standard say in this regard ?


我手边没有副本。


I don''t have my copy at hand.



在此主题中提前两小时查看我的回复。在发布之前阅读帖子中的早期回复通常是一个很好的想法。


See my reply about two hours earlier in this thread. It''s often a good
idea to read earlier replies in the thread, before posting.


>>如果发生了复制初始化,那么如果复制
ctor是''explicit'',则调用fn(obj)将不会编译。
>>If copy initialization happens, then if the copy
ctor is ''explicit'', then calling fn(obj) will not
compile.


当然可以。


Sure it will.



对不起,它只能用破坏的编译器编译(例如MSVC)。


I''m sorry, it will only compile with a broken compiler (e.g. MSVC).


>>我对么 ?
>> Am I correct ?


不。


No.



他是对的。


He is correct.


>但是,如果声明了复制构造函数显式,


测试copy_init = obj;

将无法编译。 (VC ++ 2005说:
类''测试'':没有可用的复制构造函数或
复制构造函数被声明''显式''
>However, if the copy constructor is declared explicit,
the line

Test copy_init = obj;

will not compile. (VC++ 2005 says:
class ''Test'' : no copy constructor available or
copy constructor is declared ''explicit''


>>请解释
>>Kindly explain


运行我的示例。


Run my example below.



当我添加单词明确到你的

程序的复制构造函数,并注释掉copy_init声明,

Comeau C / C ++ 4。3。9(2007年3月27日17: 24:47)for ONLINE_EVALUATION_BETA1

版权所有1988-2007 Comeau Computing。保留所有权利。

模式:严格错误C ++ C ++ 0x_extensions


" ComeauTest.c",第47行:错误:类测试没有合适的副本

构造函数

fn(obj);

^

在ComeauTest.c的编译中检测到
1错误。


g ++也无法编译,而Visual C ++确实编译了它。


When I added the word "explicit" to the copy constructor of your
program, and commented out the copy_init declaration,

Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 47: error: class "Test" has no suitable copy
constructor
fn(obj);
^

1 error detected in the compilation of "ComeauTest.c".

g++ also failed to compile, whereas Visual C++ did compile it.



您应该检查您正在使用哪些标志,我无法通过VC ++编译器获得




-

Erik Wikstr ?? m

You should check which flags you are using then, I could not get that
past the VC++ compiler.

--
Erik Wikstr??m


su ************** @ yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:

测试direct_init(obj); //直接初始化发生在这里

测试copy_init = obj; //复制初始化发生在这里
Test direct_init(obj); // direct initialization happens here
Test copy_init = obj; // copy initialization happens here



这些行完全相同。后者只是语法上的前者糖的价值。

Those lines do the exact same thing. The latter is just syntactic
sugar for the former.


这篇关于初始化函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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