如何用方法包装一些代码? [英] How to wrap some code with a method?

查看:95
本文介绍了如何用方法包装一些代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何用方法包装一些代码?

例如我有以下代码:

Hi,

How can wrap some code with a method?

for example I have this code:

if(x=y)
{
  agent.gotox();
}



我想把它放在一个方法中它不在main中,而在其他.cpp文件中.

谢谢



and I want to put this in a method. It''s not in main and its in other .cpp file.

Thanks

推荐答案

请不要在CodeProject上停止在此处提出任何问题,直到您使用一些基本的书来学习编程的基础知识并就以下主题的所有主题进行最简单的练习,例如C ++.它应该给您足够的信心;您还可以收集一些更明智的问题.

当您获得这种经验时,欢迎回来.这只会使CodeProject成员提供的所有帮助更加有效.现在,这太浪费时间了.

谢谢您的理解.

—SA
Please kindly consider to stop asking any questions here at CodeProject until you learn the very basics of programming using some elementary book and do most simple exercises on all topic of, say, C++. It should give you enough confidence; and you also will be able to collect some more sensible questions.

When you get this experience, welcome back. This would only make all kind of help from CodeProject members much more effective. Right now, it''s too much of time loss.

Thank you for your understanding.

—SA


以这种方式执行:
Do it in this way:
void my_function(int y,int& x)
{
  if(x=y)
  {
    agent.gotox();
  }
}


但请记住:
if(x=y)就像x=y; if(0!=x) //...
问候.


but remember:
if(x=y) is like x=y; if(0!=x) //...
Regards.


什么类的方法? agent 对象与此类的关系如何?
如果agent是该类的数据成员,则可以使用类似于以下内容的内容:
Method of what class? And how is the agent object related to such class?
If the agent is a data member of the class then you may use something similar to:
//MyClass header file
class MyClass
{
//...
Agent agent;
public:
  void checkAndGo(int x, int y);
//...


// MyClass source file
void MyClass::checkAndGo(int x, int y)
{
  if (x==y)
    agent.goto(x);
}


另一方面,如果agent不是该类的成员,则您必须更改该方法的签名,例如:


on the other hand, if agent isn''t member of the class then you''ve to change the method''s signature, for instance:

void checkAndGo(int x, int y, Agent & agent);


这篇关于如何用方法包装一些代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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