简体   繁体   中英

How can I place function's return value to new variable c++

So I want to modify Class' private parts information by placing functions (split) return value( type is (vector strin?, string)) to reunat_(vector) and palankuva_(string) which are Class' variables. How and where do I define variable "tallennettava" so that i can assign the values function "split()" returns first to the "tallennettava" variable and from there copy the values to the Class' variables reunat_ and palankuva_. The code seen below assing to the variable "tallennettava" empty variables reunat and kuva and after I try to assign the "split()" functions return value to it, it can't be done since the "tallennettava" variable is already "full" from the empty variables reunat and kuva. Sorry about the non-english variable names. :(

void Pala::tallenna_pala(string komento)
{
    vector<string> reunat;
    string kuva;
    string palantiedot;

    char erotinmerkki;
    erotinmerkki = (':');
    reunat_.clear();
    palankuva_.clear();

    if ( komento.length()> 23)
    {
        if ( patki_komento(komento, palantiedot)==true ) 
        {
            Pala tallennettava {reunat, kuva};
            tallennettava = split(palantiedot,erotinmerkki);

            reunat_ = reunat;
            palankuva_ = kuva;
            cout << reunat.at(1)<<endl;
        }
        else 
        {
            cout << "Virheellinen syote" << endl;
        }
    }
}

You probably want something like this:

    *this = split(palantiedot, erotinmerkki);

I say this because split seems to return something from which a Pala can be constructed, so it's easier to just assign it to *this than to capture the result and then copy the fields one-by-one.

As you are within Pala class, you can access private members of any Pala object, so just do this:

    Pala tallennettava = split(palantiedot,erotinmerkki);
    reunat_ = tallennettava.reunat_;
    palankuva_ = tallennettava.palankuva_;

Also, never do if ( cond == true ) , prefer if ( cond ) which is safer, as there is no standard definition of what's the value of true ( false is 0 , true is anything else, so ( 3 == true ) could be evaluated to false when ( 3 ) will be evaluated to true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM