View Full Version : Get out a variable from function (C++)
ghostlake114 11-25-2008, 06:34 AM This is not purely a Maya API thing, but C++ more :curious:
I have a function like this
void vietra()
{
MString kuka = "dsdsds";
MGlobal::displayInfo(kuka );
}
Now how could I get the variable kuka from this function to use in another function, without use the return way
Thanks
|
|
RyanT
11-25-2008, 07:52 AM
I'm not an expert in C++ but, you cant get a variable out of a function without passing it a reference or returning a value, or I suppose if kuka was a global variable another function could get to it as well. You could also create a class.
Option 1:
Return a value
MString vietra()
{
MString kuka = "dsdsds";
MGlobal::displayInfo(kuka );
return kuka;
}
Option 2:
Pass a reference of your variable to the function
MString kuka;
void vietra( MString &kuka )
{
kuka = "dsdsds";
MGlobal::displayInfo( kuka );
}
Option 3:
// Create your class
class yourclass
{
// This is a private variable
// not accessable outside of your class
MString kuka;
// Declare your functions you want to have
public:
MString vietra( MString value );
void display_kuka();
};
// Define what the function declared in the class will do
void yourclass::vietra( MString value )
{
// this changes kuka and it will stay this value
kuka = value;
MGlobal::displayInfo( kuka );
}
void yourclass::display_kuka()
{
MGlobal::displayInfo( kuka );
}
int main ()
{
yourclass newobj;
MString newval = "dsdsds";
newobj.vietra( newval );
newobj.display_kuka();
}
I dont have a way to test this code but it should work. At least it will give you an idea as to what to look up info on.
Hope that made sense.
ghostlake114
11-25-2008, 09:30 AM
Thanks, the 3rd option is what I am looking for because It is more C++ way.
Again, thanks for your help, maybe I will continue mess up the community with some basic question like this for a while :blush::blush:
CGTalk Moderation
11-25-2008, 09:30 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.