+ 1

Return char as the Char class(my class)

I want to return a char of a std::string as reference object of my class What to do?

26th Nov 2020, 10:44 AM
master.cpp
6 Respostas
+ 4
You wrote, I will reformulate: I want my class to return a reference object to std :: string ... If you mean something else, be clearer. There are practically no psychics here, I have not met ...
26th Nov 2020, 11:31 AM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 3
char& yourClass::getStrPropCharR(int i){ return *(m_StrProp.begin() + i); }; ... or char* yourClass::getStrPropCharP(int i){ return (m_StrProp.begin() + i); }; ... /////// But working with the std :: string type this way is not very good ... there will be problems when modifying a string through its native methods and through modifying a separate character .... it is better to leave std :: string and use std :: vector <char>
26th Nov 2020, 1:26 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 2
try it: class yourClass{ ... public: std::string& getStrProp(); ... private: std::string m_StrProp; ... }; std::string& yourClass::getStrProp(){ return m_StrProp; };
26th Nov 2020, 11:07 AM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 1
Why you want to wrap a char inside your class? why can't you just return the char? You can design your class' constructor to take a char, and store it as value of a private member. That way you can instantiate your class using the char taken off a std::string. Maybe you need to elaborate further on the idea of this class.
26th Nov 2020, 1:25 PM
Ipang
0
I wrote I need return reference of an element of std::string But std::string's elements are char I need to convert it to MyChar class But it still bee a reference I mean anyone can change that element
26th Nov 2020, 1:16 PM
master.cpp
- 3
This is not my answer
26th Nov 2020, 11:27 AM
master.cpp