You are not logged in.

#1 2026-05-23 15:20:46

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

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 (2026-05-23 15:24:09)

Offline

#2 2026-05-23 15:27:40

5hridhyan
Member
Registered: 2025-12-25
Posts: 1,145

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 (2026-05-23 15:33:41)


Gentoo is where dreams and compile times go to die.
And you'll definitely find me talking to a wall :-)

Offline

#3 2026-05-23 15:36:41

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

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 2026-05-23 19:48:56

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

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 2026-05-23 20:55:15

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

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 (2026-05-23 20:56:11)

Offline

#6 2026-05-23 21:00:59

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

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 2026-05-23 21:22:51

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

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

#8 Today 10:13:19

HBerger
Member
Registered: 2026-02-18
Posts: 2

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

yes, it is possible.

Don't think the answer should be that easy, even if it hits in 98+ % of the cases :-)

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

Strings are templates, and template alignment are heavily depending on compiler, versions, and flags. C++ has no binary API.

meanwhile c++ name mengling and member method layout is consist between compiler families that have lead to pure abstract class interfaces working across compile units as long as you use the same compiler family. In this environment template classes are more fragile. (you have to watch compiler versions and flags)

for whats relevant:
- plugin development, if wonna have at least class interfaces (pure abstract) in the extern interfaces and you dont have all the plugins under (compile) control (you should even consider c interfaces in this case :-) but i have seen lot of projects)
- if you have to switch yourself to different compile units, aka wonna using 2 libraries that demand incompatible compiler flags -> going for dynamic objects.

for internal usage (interfaces inside the same compile units), you dont have to be care ...

Last edited by HBerger (Today 10:16:03)

Offline

#9 Today 13:45:49

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

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

HBerger wrote:

Strings are templates

It doesn't matter in context of OP question. std::string is std::basic_string<char> specialization and it is self-contained class. Of course, unless you've overriden allocator with some quirky custom one.

Offline

Board footer

Powered by FluxBB