通过参考函数问题 [英] Pass by Reference Function Question

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

问题描述

我用C ++源代码编写我的大项目。我的C ++源代码

包含大约四千个小函数。其中大多数是内联的
。我在全局范围内定义变量和函数。全局

变量和全局函数被隐藏,以防止由

程序员访问。所有全局函数都共享全局变量。

只允许极少数全局函数为

程序员使用而重用。很少有全局函数在执行期间访问大多数隐藏函数



我的问题是 - 你认为通过引用传递真的是

必要?除非您想通过选择全局/局部变量重用函数

,否则必须通过引用传递。请指教。


-


你真的,

Bryan Parkoff

I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope. The global
variables and global functions are hidden to prevent from accessing by the
programmers. All global functions share global variables.
Only very few global functions are allowed to be reusability for the
programmers to use. Few global functions access most hidden functions
during the time execution.
My question is -- do you think that pass by reference is really
necessary? Pass by reference is necessary unless you want to reuse function
with the choice of global / local variables. Please advise.

--

Yours Truly,
Bryan Parkoff

推荐答案

Bryan Parkoff写道:
Bryan Parkoff wrote:

我用C ++源代码编写我的大项目。我的C ++源代码

包含大约四千个小函数。其中大多数是内联的
。我在全局范围内定义变量和函数。隐藏了

全局变量和全局函数,以防止程序员访问
。所有全局函数共享全局

变量。只有极少数全局函数可供程序员使用的可重用性。在执行时间内,很少有全局函数访问

大多数隐藏函数。
I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope. The
global variables and global functions are hidden to prevent from
accessing by the programmers. All global functions share global
variables. Only very few global functions are allowed to be
reusability for the programmers to use. Few global functions access
most hidden functions during the time execution.



听起来这是一个非常糟糕的设计。没有冒犯。

That sounds like a very bad design. No offence.


我的问题是 - 你认为通过引用传递真的是

必要吗?除非您想通过选择全局/局部变量重用

函数,否则必须通过引用传递。请指教。
My question is -- do you think that pass by reference is really
necessary? Pass by reference is necessary unless you want to reuse
function with the choice of global / local variables. Please advise.



你需要一本关于设计的好书并有时间来消化它。我不是

真的确定要推荐什么。 高级C ++作者:Coplien?日期。

多范式设计用于C ++由他?也许。 现代C ++设计

by Alexandrescu? 大规模C ++软件设计拉科斯? < shrug>


如果你想要更多的OOD理论,请寻找Grady Booch(并在

comp.object新闻组中询问)。


我读过和教过的大部分内容都说明全球数据应该是b
。因此,如果你从这个前提开始,将对象传递给你的

函数(通过指针或引用)就变得必不可少了。


V
-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要不要问

You need a good book on design and some time to digest it. I am not
really sure what to recommend. "Advanced C++" by Coplien? Dated.
"Multiparadigm Design for C++" by him? Maybe. "Modern C++ Design"
by Alexandrescu? "Large-Scale C++ Software Design" by Lakos? <shrug>

If you want more OOD theory, look for Grady Booch (and ask in the
comp.object newsgroup).

Most of what I''ve read and been taught says that global data should be
avoided. So, if you start with that premise, passing objects to your
functions (either by pointer or by reference) becomes a necessity.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


4月11日下午3:24,Bryan Parkoff < nos ... @nospam.comwrote:
On Apr 11, 3:24 pm, "Bryan Parkoff" <nos...@nospam.comwrote:

我用C ++源代码编写我的大项目。我的C ++源代码

包含大约四千个小函数。其中大多数是内联的
。我在全局范围内定义变量和函数。
I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope.



OK

OK


全局

变量和全局函数被隐藏到阻止

程序员访问。
The global
variables and global functions are hidden to prevent from accessing by the
programmers.



那么他们不是全球性的吗?

你用什么方法来隐藏?他们?

Then they aren''t global are they?
What means did you use to "hide" them?


所有全局函数共享全局变量。
All global functions share global variables.



OK

OK


只有极少数全局函数可以重复使用

程序员使用。
Only very few global functions are allowed to be reusability for the
programmers to use.



你是什么意思?

你有全局功能,你不想让任何人打电话?他们

可能不应该是全球性的。

What do you mean?
You have global functions that you don''t want anyone to call? They
probably shouldn''t be global then.


很少有全局函数可以访问大多数隐藏的函数

在执行期间。
Few global functions access most hidden functions
during the time execution.



我认为全局函数都隐藏在你之前所说的
之前......

所以,你有全球性的隐藏函数(我不知道如何)调用

其他全局隐藏功能?

I thought the global functions were hidden themselves from what you
said earlier...
So, you have global "hidden" functions (I don''t know how) calling
other global "hidden" functions?


我的问题是 - 您认为通过引用传递是否真的需要

My question is -- do you think that pass by reference is really
necessary?



需要什么?

你想做什么?显示一些源代码。

Necessary for what?
What are you trying to do? show some source code.


除非你想重用函数

并选择global /局部变量。
Pass by reference is necessary unless you want to reuse function
with the choice of global / local variables.



什么?我不知道你在说什么,但没有。根据全局函数或

变量或缺少变量,没有

要求通过引用传递。

What? I don''t know what you are talking about, but no. There is no
requirement to pass by reference based on global functions or
variables or lack thereof.


On 4月11日下午3:24,Bryan Parkoff < nos ... @ nospam.comwrote:
On Apr 11, 3:24 pm, "Bryan Parkoff" <nos...@nospam.comwrote:

>我用C ++源代码编写我的大项目。我的C ++源代码
包含大约四千个小函数。其中大多数都是内联的。我在全局范围内定义变量和函数。
> I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope.



OK


OK



我看到你现在明白了。

I see you understand now.


>全局
变量和全局函数被隐藏,以防止程序员访问

>The global
variables and global functions are hidden to prevent from accessing by
the
programmers.



那么他们不是全球性的吗?

你用什么方法来隐藏?他们?


Then they aren''t global are they?
What means did you use to "hide" them?



好​​吧,你可以做出两个选择之一 - 全局对象或本地对象。

本地对象非常适合课程。你总是可以在类中绑定局部变量和

本地函数。我选择使用全局对象。我同意

应该避免使用全局变量,除非你将全局对象放在

名称空间中以获得更好的可读性。全局对象比类本地对象更关注性能问题。无论如何...

Well, you can make one of two choice -- global object or local object.
The local object is ideal for class. You can always bind local variable and
local function inside class. I have chosen to use global object. I agree
that global variable should be avoided unless you put global object inside
namespace for better readability. Global object has concern of performance
issue than local object like class. Anyway...


>所有全局函数都共享全局变量。
> All global functions share global variables.



OK


OK


>只允许极少数全局函数可供程序员使用的可重用性。
> Only very few global functions are allowed to be reusability for the
programmers to use.



你是什么意思?

你有全局功能,你不想让任何人打电话?他们

可能不应该是全球性的。


What do you mean?
You have global functions that you don''t want anyone to call? They
probably shouldn''t be global then.



您可以调用公共全局函数。然后公共全局函数反过来调用隐藏的全局函数
。我不希望程序员看到

隐藏的全局函数,并且允许他们通过运行

公共全局函数来使用全局对象。

You call public global function. Then public global function is in turn
to call hidden global functions. I do not want the programmers to see
hidden global functions and they are allowed to use global object by running
public global function.


>在执行时间内,很少有全局函数访问大多数隐藏函数。
>Few global functions access most hidden functions
during the time execution.



我认为全局功能隐藏在你之前所说的b $ b ...

所以,你有全球性的隐藏函数(我不知道如何)调用

其他全局隐藏功能?


I thought the global functions were hidden themselves from what you
said earlier...
So, you have global "hidden" functions (I don''t know how) calling
other global "hidden" functions?


>我的问题是 - 您认为通过引用传递真的是必要的吗?
>My question is -- do you think that pass by reference is really
necessary?



需要什么?

你想做什么?显示一些源代码。


Necessary for what?
What are you trying to do? show some source code.


>除非您想重用函数
并选择全局/局部变量,否则必须按引用传递。
>Pass by reference is necessary unless you want to reuse function
with the choice of global / local variables.



什么?我不知道你在说什么,但没有。根据全局函数或

变量或缺少变量,没有

要求通过引用传递。


What? I don''t know what you are talking about, but no. There is no
requirement to pass by reference based on global functions or
variables or lack thereof.



我来给你一些示例代码。它可以帮助您更好地理解。你可以看到
可以看到没有必要通过引用传递,因为所有隐藏的函数

共享全局变量。


//全局对象

命名空间对象

{

static int a = 0; //隐藏的全局变量

static int b = 0; //隐藏的全局变量

static int c = 0; //隐藏的全局变量


static void Modify1(void); //隐藏的全局函数

static void Modify2(void); //隐藏的全局函数

static void Modify3(void); //隐藏的全局函数

void Run_Object(void); //公共全球功能


void Modify1(无效)

{

a + = 2;

b + = 4;

c = a * b;

}


void Modify2(无效)

{

a * = 5;

b * = 10;

c = a - b;

}


void Modify3(无效)

{

a = b / c;

b = 0;

c = 0;

}


void Run_Object(无效)

{

Modify1();

Modify2();

Modify3();

}

}


//标题末尾


#include" object.h" //上面的代码


int main(无效)

{

//做点什么......

Object :: Run_Object(); //对所有隐藏函数都运行良好。


Object :: Modify1(); //错误时间编译 - 你无法访问隐藏的

全局函数。

a = 5; // Time Compile运行正常,除非你使用全局变量 - 这个源上的main函数外面的一个
,比如main.cpp

Object :: a = 10; //错误时间编译 - 你无法访问隐藏的全局

变量

返回0;

}


您可以看到该命名空间是可读性的理想选择,因为有时

全局变量在两个源代码中都具有相同的名称。它防止意外覆盖



请指教。


你真的,

Bryan Parkoff

Let me give you sample code. It helps you to understand better. You
can see that pass by reference is not necessary because all hidden functions
share global variable.

// Global object
namespace Object
{
static int a = 0; // hidden global variable
static int b = 0; // hidden global variable
static int c = 0; // hidden global variable

static void Modify1 (void); // hidden global function
static void Modify2 (void); // hidden global function
static void Modify3 (void); // hidden global function
void Run_Object (void); // public global function

void Modify1 (void)
{
a += 2;
b += 4;
c = a * b;
}

void Modify2 (void)
{
a *= 5;
b *= 10;
c = a - b;
}

void Modify3 (void)
{
a = b / c;
b = 0;
c = 0;
}

void Run_Object (void)
{
Modify1();
Modify2();
Modify3();
}
}

// end of header

#include "object.h" // above code

int main (void)
{
// do something...
Object::Run_Object(); // Run fine for all hidden functions.

Object::Modify1 (); // Error Time Compile -- you can''t access hidden
global functions.
a = 5; // Time Compile runs fine unless you use global variable -- a
outside main function on this source like main.cpp
Object::a = 10; // Error Time Compile -- you can''t access hidden global
variable
return 0;
}

You can see that namespace is ideal for readability because sometimes
global variable has the same name in both source codes. It guards against
overwritten accidently.
Please advise.

Yours Truly,
Bryan Parkoff


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

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