0

im trying to return a local variable from a function.

typedef struct _s{
  int a;
} Somestruct;

Somestruct func(){
  Somestruct RET;
  RET.a = 10;
}

int main(){

  Somestruct var = func();
  std::cout << var.a();
}

will this work? if i doesnt, what do i need to do to make it return the value i want? i have tried to do this with classes but it doesnt work. From the info i have learnt, when i return classes it just returns the pointer that points to the memory. However, i was thinking if i did structs, then it would physically return the whole data, not just a reference or pointer.

a276me
  • 27
  • 4
  • 2
    Where in `func` do you `return` anything? – NathanOliver Aug 26 '21 at 14:27
  • sorry, a mistake, i meant to return something – a276me Aug 26 '21 at 14:28
  • "From the info i have learnt, when i return classes it just returns the pointer that points to the memory." That is wrong. When you return an object then an object is returned, not a pointer – 463035818_is_not_an_ai Aug 26 '21 at 14:28
  • No it doesn't return a pointer. In most cases the compiler will actually know it should allocate the object at the caller, and then let the function fill it. The details are described here : https://en.cppreference.com/w/cpp/language/copy_elision – Pepijn Kramer Aug 26 '21 at 14:33
  • 2
    Looks like you are learning C++ after learning C. Change this C-style type `typedef struct _s { int a; } Somestruct;` to C++ `struct Somestruct { int a; };` – Eljay Aug 26 '21 at 14:33

3 Answers3

2

what do i need to do to make it return the value i want?

You need to literally ask the program to do that using return RET;

typedef struct _s{
  int a;
} Somestruct;

Somestruct func(){
  Somestruct RET;
  RET.a = 10;
  return RET;  // <====
}

when i return classes it just returns the pointer that points to the memory

No, C++ has value semantics. Returning a struct/class by-value will return the struct itself.

There is no difference between a struct and a class with this respect.


Bonus note: var.a() is not a function, you probably want var.a.

int main(){

  Somestruct var = func();
  std::cout << var.a << "\n";  // <====
}
rustyx
  • 80,671
  • 25
  • 200
  • 267
2

will this work?

No. There are severe issues:

  • Function has been declared to return an object, but there is no return statement (nor a throw nor anything that would terminate the program). The behaviour of the program is undefined.
  • _s::a is not callable, so var.a() is ill-formed.
  • The identifier _s is reserved to the language implementation in the global namespace. By defining it, the behaviour will be undefined.

what do i need to do to make it return the value i want?

A return statement.

From the info i have learnt, when i return classes it just returns the pointer that points to the memory.

You've learned wrong. When you return instance of a class, you return a copy of the value (this is somewhat simplified description for the benefit of a beginner reader).

However, i was thinking if i did structs

Structs are classes too.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Further explanation of the problem with `_s`: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Aug 26 '21 at 15:36
1

It works if you modify func() like so:

Somestruct func(){
  Somestruct RET;
  RET.a = 10;
  return RET;
}

For your second point, you can definitely return classes, you just have to be careful to return the actual object like this myClass myFunc() and not through a pointer myClass* myFunc() nor through a reference myClass& myFunc()

Tzig
  • 726
  • 5
  • 20