You are not logged in.

#1 Yesterday 15:20:46

lo7777799
Member
Registered: 2023-07-25
Posts: 86

c++ - strings as return of a function? is that always possible?

good afternoon to you all,

i have a question ... is it always possible to use a string as a return of a function? is that also possible in a class?

class my_class{

private: 
int var_one = 0 ; 
string my_string = 'mytext' ; 

public: 
string returning_function(){
return my_string ; 
}

int main(){
new_string = 'text' ; 
new_string = myclassobject.returning_function(); 
return 0 ; 

would something similar to that code with a string as return run without problems?

Last edited by lo7777799 (Yesterday 15:24:09)

Offline

#2 Yesterday 15:27:40

5hridhyan
Member
From: Asia
Registered: 2025-12-25
Posts: 759

Re: c++ - strings as return of a function? is that always possible?

Yup. And your code has some syntax issues, single quotes (') are strictly for single characters (like 'a'). For strings of text, you must use double quotes ("mytext")
also it doesn't have a built-in string type by default; it lives in the Standard Library. You need to #include <string> and use std::string
the main() function cannot sit inside your class definition. It needs to be outside, and it needs to create an object of your class to call that function
also you need to give new_string a type (like std::string) before using it...

Edit:
also your class definition itself is  missing the closing };

Last edited by 5hridhyan (Yesterday 15:33:41)


"Nothing matters" -a Nihilist
"Why bother thinking what matters?" -me

Offline

#3 Yesterday 15:36:41

cryptearth
Member
Registered: 2024-02-03
Posts: 2,137

Re: c++ - strings as return of a function? is that always possible?

if the extension to this question will be something like "return arbitrary types - or a string" the answer is: no / only with an encapsulating additional type

to turn your question upsidedown: what is what you want to accomplish?
i smell an XY-problem

Offline

#4 Yesterday 19:48:56

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,510

Re: c++ - strings as return of a function? is that always possible?

@lo7777799 - which book covering the c++ basics have you read?
Why are you whether pseudo-code that will under no circumstances compile "will work"? It won't. It will not even compile.
Fixing it will make it work, yes - but that's a tautology.

Offline

#5 Yesterday 20:55:15

lo7777799
Member
Registered: 2023-07-25
Posts: 86

Re: c++ - strings as return of a function? is that always possible?

the code there is a pseudo-code, that i did not copy from my editor. i wrote it directly into this post above. it was to explain the question. the code there above is not to write a program.

if everybody understand my questions, then the code did that, that it must do.

"similar" does not mean "exactly".

Last edited by lo7777799 (Yesterday 20:56:11)

Offline

#6 Yesterday 21:00:59

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,510

Re: c++ - strings as return of a function? is that always possible?

It's completely pointless to ask whether some pseudo-code will work w/o problems - it won't work at all.
Coming back to

cryptearth wrote:

to turn your question upsidedown: what is what you want to accomplish?
i smell an XY-problem

Why would you even ask this? What makes you think you could *not* return a string? Or really anything (provided proper implementation)

Offline

#7 Yesterday 21:22:51

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 616

Re: c++ - strings as return of a function? is that always possible?

lo7777799 wrote:

is it always possible to use a string as a return of a function?

It depends on what you mean by string here. If you mean std::string, i.e. string class from std namespace then yes, it is possible.

lo7777799 wrote:

is that also possible in a class?

Class member functions are no different from regular functions in this regard.

lo7777799 wrote:

would something similar to that code with a string as return run without problems?

What specific problems do you expect when returning a string from a function?

Offline

Board footer

Powered by FluxBB