开发者

Calling functions and returning values

I am a beginner in C++.I have a function which returns some parameters that i need ,to use in the rest of my program.I am trying to access开发者_JAVA百科 that function within another class.I am confused with the way i can do it...Can anyone please help me..?

Following is my code :

 void SampleProgram :: myFunction()
   {
         string sInput;
         GetInfo  getInfo(sInput);  //creating instance of the class containing the function

            string sSw="";
       string sName="ram";
       string sList="list";

           getInfo.getRequiredInfo(sSw,sName,sList); //calling the function

    }

How can i access the output parameters of getRequiredInfo() ..?and save it if the ouput parameters are as following :string name,int status Please help me...


You'd have to show us the declaration of getRequiredInfo(), but presumably it's something like

X result = getInfo.getRequiredInfo(sSw,sName,sList);

We don't know what X is without seeing that declaration.


In case getRequiredInfo get the address of the strings, try this:

getInfo.getRequiredInfo(&sSw, &sName, &sList);

Now the function can change the value of the arguments.


You can able to return single value in function.

so You can create struct containing string and int and return that as the result.

    struct
    {
           string name;
            int    k;
    }result;
    result r = getInfo.getRequiredInfo(sSw,sName,sList); //calling the function

you can save the output parameters like these:

   string _name;
   int       nk;

   _name = r.name;
   nk    = r.k;


The simplest would be return a pair<string,int> from the function. So your function signarutre would be pair<string,int> getRequiredInfo(const string& s1, const string& s2, constr string& s3);. You can then access the string and int part usinf first and second members of the pair.


It sounds as though getRequiredInfo has the behaviour of both reading from and writing to some of the parameters you've passed in.

In C++, unlike in C#, there's no way to specify that a function should, or must, change the value of a given parameter. Indeed, if you have defined your function like this:

void GetInfo::getRequiredInfo(string a, string b, string c);

then all three parameters will be passed by value meaning any changes made to them inside the function will be changing copies of those objects, rather than the objects themselves.

As other contributors have suggested, if you really want to change the parameters you passed in, you could either do so by passing references:

void GetInfo::getRequiredInfo(string a, string& b, string& c);

or (more typically) by passing pointers:

void GetInfo::getRequiredInfo(string a, string* b, string* c);

However, if you want your function to simply read the parameters and return a value (which is the more accepted term for what you're calling 'output parameters') then you have to express your tuple of "name + status" as a single value.

You could use the built in pair template as someone else just suggested:

pair<string,int> getRequiredInfo(string a, string b, string c);

Or you could define your own struct to do it, like someone else suggested:

struct NameAndStatus
{
    string name;
    int status;
};

NameAndStatus getRequiredInfo(string a, string b, string c);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜