从另一个函数访问一个函数的变量 [英] Accessing a variable of one function from another

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

问题描述

在代码功能中:

1).h文件:

In a code function:

1).h file:

class Test1
{
void function1();

void function2();
}



2).cpp文件:



2) .cpp file:

Test1::function1()
{
double val = 20.88;
}


Test2::function2()
{
// how to access the val from the function1??
}



这行



The line

Test1::function1-> val 

给出错误.

推荐答案

您不能.

它是一个局部变量,这意味着它仅在函数实际执行时存在.函数结束时,它超出范围,不再可用.

在现实世界中,情况实际上更糟:变量本身是在堆栈上创建的,并且当函数执行结束时,会释放堆栈空间,因此变量值实际上被需要该空间的下一个函数破坏.这就是悬挂或悬挂的引用 [
You can''t.

It''s a local variable, which means it only exists when the function is actually being executed. At the end of the function it goes out of scope and it no longer available.

In the real world, it''s actually worse: the variable itself is created on the stack, and when the function execution ends the stack space is released so the variable value is actually destroyed by the next function that needs the space. This is what hanging or dangling references[^] are all about: accessing variables outside a function that do not exist once the function ends.


格里夫之后" s和nv3的答案还有很多话要说...但是我还是要说!

在C ++中,函数不是其他编程语言中的对象.除了使用一组特定的参数执行函数的结果之外,您不能问函数什么.

nv3提到您可以使val成为Test1的成员,并将值保留在那里并可以在其中访问.如果那对您不起作用,则可以构建像函数一样工作的对象.这些在文献中被称为函子-快速搜索,您将发现有关它们的各种有趣的东西.

简而言之,函子是一个带有重载函数调用运算符的对象.这使您具有以下功能:

-通话之间有状态
-可以复制(听起来并不酷,但是请相信我)

您可以使用三个类而不是一个类来获得所需的内容:
After Griff''s and nv3''s answers there''s not a lot left to say... But I''ll say it anyway!

In C++ functions aren''t objects the way in some other programming languages. You can''t ask a function anything apart from what the result of executing it is with a particular set of parameters.

nv3 mentioned that you could make val a member of Test1 and keep the value there and have it accessible there. If that''s not going to work for you you can build objects that work like functions. These are called functors in the literature - do a quick search and you''ll find out all sorts of interesting stuff about them.

In a nutshell a functor is an object with an overloaded function call operator. This lets you have a function that:

- has state between calls
- can be copied (this doesn''t sound that cool, but it is, trust me)

You can get what you seem to want using three classes instead of one:
struct f1
{
	double val;

	f1::f1() : val( 0 ){}

	void operator()()
	{
		val = 20.88;
	}
};

struct f2
{
	f1 *function1;

	f2( f1 * function1_ ) : function1( function1_ ){}

	void operator()() const
	{
		std::cout << function1->val << std::endl;
	}
};

class Test1
{
	public:
		Test1() : function2( &function1 ){}

		f1 function1;
		f2 function2;
};

在这里,我制作了f1f2仿函数,它们执行您希望Test1的成员函数执行的操作. function1现在可以独立于Test1维护某些状态.

您可以看到它的工作方式如下:

Here I''ve made f1 and f2 functors that do the sort of thing you wanted the member functions of Test1 to do. function1 can now maintain some state independently of Test1.

You can see it work with something like:

int main()
{
	Test1 t1;

	t1.function2();
	t1.function1();
	t1.function2();
}

function2()第一次输出零,第二次输出20.88.

太酷了,所以可以使用.但这比nv3的示例复杂得多.什么时候使用这个更复杂的代码块?答案并不常见,但是它确实具有nv3的代码无法管理的用途-您可以使用Test1的组件而无需使用Test1,将它们组成任意类.它们还可以充当撕纸",使您仅在使用大块代码和数据时才能实例化它们.当您需要通用的代码作为成员函数时,也可以使用这种东西而不是实现继承.它们还提供了很好的模板参数,从而暴露了特定的功能以及该功能.

但是,如果您经常(即根本)需要它们,那么您可能会缺少一些东西-请回去检查您的设计!

function2() outputs a zero first time around, 20.88 the second time.

Cool, so it works. But it''s a lot more complicated than nv3''s example. When would you use this more complicated styke lump of code? The answer is not often but it does have one use that nv3''s code can''t manage - you can use the components of Test1 without needing a Test1 around, composing them into arbitrary classes. They can also act as "Tear offs" enabling you to instantiate large chunks of code and data only when they''re used. You can also use this sort of thing instead of implementation inheritance when you need a common piece of code as a member function. They also make good template parameters, exposing a particular piece of functionality and just that piece.

However if you need them that often (i.e. at all) then you''re probably missing something - go back and review your design!


只是对前两篇文章的修正:您可能想要做的是使此变量成为您的Test1类的成员变量.例如这样的:

Just as an amendment to the two previous posts: What you probably want to do is to make this variable a member variable of your class Test1. For example like this:

class Test1
{
private:
    double m_val;

public:
    Test1()
    {
        m_val = 0.0;
    }

    void function1()
    {
        m_val = 20.88;
    }
 
    void function2()
    {
        double y = m_val;
        // ...
    }
};



那可能就是你想要的.或者,如果没有类Test1的实例,则可以将其设为静态成员:



That is what you probably want. Or as an alternative, in case you do not have an instance of class Test1, you can make it a static member:

class Test1
{
private:
    static double s_val;

public:
    void function1()
    {
        s_val = 20.88;
    }

    void function2()
    {
        double y = s_val;
        // ...
    }
};

double Test1::s_val = 0.0;



也许可以使您更进一步.



Perhaps that get''s you a step further.


这篇关于从另一个函数访问一个函数的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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