You are not logged in.
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
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
Online
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
@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
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
It's completely pointless to ask whether some pseudo-code will work w/o problems - it won't work at all.
Coming back to
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
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.
is that also possible in a class?
Class member functions are no different from regular functions in this regard.
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