[try Beta version]
Not logged in

 
object type method return

Apr 29, 2014 at 5:25am
hi guys i am new to c++ and trying to learn. for instance. i have a struct and method.I am trying to learn what i can do with the method if i define the return type as struct type.

1
2
3
4
5
6
7
8
9
10
    struct S
    {
         int age;
         string name;
    };
    S method()
    {
    //what i can do in here. with the Struct. I mean can i reach members of the struct. etc
    }
Apr 29, 2014 at 5:50am
method() is not a method. it is a freestanding function which returns S by value.
You can create S variable and return it like that:
1
2
3
4
5
6
7
8
9
10
11
12
S method()
{
    S ret;
    ret.age = 4;
    ret.name = "Fido";
    return ret; 
}
//or even
S method()
{
    return {4, "Fido"};
}
Apr 29, 2014 at 6:26am
thank you that was really helpful
Topic archived. No new replies allowed.