Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a code function:

1).h file:
C++
class Test1
{
void function1();

void function2();
}


2) .cpp file:

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


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


The line
C++
Test1::function1-> val 
gives error.
Posted

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.
 
Share this answer
 
Comments
Maciej Los 11-Jun-12 16:34pm    
Good answer, 5!
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:
C++
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;
};
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:
C++
int main()
{
	Test1 t1;

	t1.function2();
	t1.function1();
	t1.function2();
}
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!
 
Share this answer
 
Comments
Maciej Los 11-Jun-12 16:33pm    
Good explanations, my 5!
stib_markc 13-Jun-12 5:50am    
Comprehensive, my 5!
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;
        // ...
    }
};


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.
 
Share this answer
 
Comments
Maciej Los 11-Jun-12 16:34pm    
Good answer, 5!
nv3 11-Jun-12 16:54pm    
Thanks!
Sumal.V 12-Jun-12 6:05am    
Cheers :)
The scope of local variable is within the function, you can not access that variable outside the function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900