C ++:未初始化的变量作为Parm传递 [英] C++: Uninitialised Variable Passed as a Parm

查看:52
本文介绍了C ++:未初始化的变量作为Parm传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下保证安全吗?


void InitInt(int& SomeInt)

{

SomeInt = 3;

返回;

}


int main()

{

int MainInt; //未初始化!

InitInt(MainInt);

std :: cout<< MainInt<< std :: endl;

返回0;

}


函数的意图是初始化它的parm,为什么

首先在main()中初始化它?当然,除非它不是安全的b $ b。安全吗?


此致,


Gene Wirchenko

解决方案

2003年12月17日星期三18:18:33 GMT,Gene Wirchenko

< gw ************** @ CAPITALSwencomine。 COM>写道:

以下保证安全吗?


哎呀!我忘了

#include< iostream>

void InitInt(int& SomeInt)
{
SomeInt = 3;
返回;
}
int main()
{
int MainInt; //未初始化!
InitInt(MainInt);
std :: cout<< MainInt<< std :: endl;
返回0;
}

该函数的目的是初始化它的parm,为什么
首先在main()中初始化它?当然,除非它不安全。安全吗?




此致,


Gene Wirchenko




" Gene Wirchenko" < GW ************** @ CAPITALSwencomine.com> schrieb im

Newsbeitrag新闻:sd ******************************** @ 4ax.com ...

2003年12月17日星期三18:18:33 GMT,Gene Wirchenko
< gw ************** @ CAPITALSwencomine。 COM>写道:

以下保证安全吗?



哎呀!我忘了
#include< iostream>

void InitInt(int& SomeInt)
{
SomeInt = 3;
返回;
}
int main()
{
int MainInt; //未初始化!
InitInt(MainInt);
std :: cout<< MainInt<< std :: endl;
返回0;
}

该函数的目的是初始化它的parm,为什么
首先在main()中初始化它?当然,除非它不安全。它安全吗?




原则上它是安全的,虽然练习本身是有争议的 - 为什么不

初始化声明点?

要了解它是否有意义,您需要提供更多详细信息

您的意图。


Chris




" Gene Wirchenko" < GW ************** @ CAPITALSwencomine.com>一个écritdansle

message de news: SD ********* @ 4ax.com < /a> ...。#blockquote class =post_quotes> 2003年12月17日星期三18:18:33 GMT,Gene Wirchenko
< gw ************** @ CAPITALSwencomine.com>写道:

以下保证安全吗?



哎呀!我忘了
#include< iostream>

void InitInt(int& SomeInt)
{
SomeInt = 3;
返回;
}
int main()
{
int MainInt; //未初始化!
InitInt(MainInt);
std :: cout<< MainInt<< std :: endl;
返回0;
}

该函数的目的是初始化它的parm,为什么
首先在main()中初始化它?当然,除非它不安全。安全吗?



真诚的,

Gene Wirchenko




听起来对我来说非常安全。 ..尽管如此,我几乎没有理解这个函数的意思

:花费一个函数调用并且什么都不做

有趣......而且你不需要返回在函数InitInt中。


Is the following guaranteed safe?

void InitInt(int & SomeInt)
{
SomeInt=3;
return;
}

int main()
{
int MainInt; // not initialised!
InitInt(MainInt);
std::cout << MainInt << std::endl;
return 0;
}

The intent of the function is to initialise its parm, so why
bother initialising it first in main()? Unless, of course, it is not
safe. Is it safe?

Sincerely,

Gene Wirchenko

On Wed, 17 Dec 2003 18:18:33 GMT, Gene Wirchenko
<gw**************@CAPITALSwencomine.com> wrote:

Is the following guaranteed safe?
Oops! I forgot to
#include <iostream>
void InitInt(int & SomeInt)
{
SomeInt=3;
return;
}

int main()
{
int MainInt; // not initialised!
InitInt(MainInt);
std::cout << MainInt << std::endl;
return 0;
}

The intent of the function is to initialise its parm, so why
bother initialising it first in main()? Unless, of course, it is not
safe. Is it safe?



Sincerely,

Gene Wirchenko



"Gene Wirchenko" <gw**************@CAPITALSwencomine.com> schrieb im
Newsbeitrag news:sd********************************@4ax.com...

On Wed, 17 Dec 2003 18:18:33 GMT, Gene Wirchenko
<gw**************@CAPITALSwencomine.com> wrote:

Is the following guaranteed safe?



Oops! I forgot to
#include <iostream>

void InitInt(int & SomeInt)
{
SomeInt=3;
return;
}

int main()
{
int MainInt; // not initialised!
InitInt(MainInt);
std::cout << MainInt << std::endl;
return 0;
}

The intent of the function is to initialise its parm, so why
bother initialising it first in main()? Unless, of course, it is not
safe. Is it safe?



In principle it′s safe though the practice itself is arguable - why not
initialize at the point of declaration?
To see whether it makes sense you′d need to provide some more details of
your intention.

Chris



"Gene Wirchenko" <gw**************@CAPITALSwencomine.com> a écrit dans le
message de news:
sd********************************@4ax.com...

On Wed, 17 Dec 2003 18:18:33 GMT, Gene Wirchenko
<gw**************@CAPITALSwencomine.com> wrote:

Is the following guaranteed safe?



Oops! I forgot to
#include <iostream>

void InitInt(int & SomeInt)
{
SomeInt=3;
return;
}

int main()
{
int MainInt; // not initialised!
InitInt(MainInt);
std::cout << MainInt << std::endl;
return 0;
}

The intent of the function is to initialise its parm, so why
bother initialising it first in main()? Unless, of course, it is not
safe. Is it safe?



Sincerely,

Gene Wirchenko



sounds perfectly safe to me... nevertheless, I hardly understatnd the point
of this function : costs a function call and does almost nothing
interesting... and you don''t need the return in the function InitInt.


这篇关于C ++:未初始化的变量作为Parm传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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