G
Guest
I want to extend the string class so I wrote this code:
------------------------------
class string2 : public string
{
public:
//string2() {}
//string2(string s) { *this = s; }
string trim()
{
int st = this->find_first_not_of(" \t\r\n");
int ed = this->find_last_not_of(" \t\r\n");
if (st != npos) return this->substr(st, ed - st + 1); else return string();
}
};
------------------------------
As you see, I have no changes in class data (extra variables, virtual functions etc)
I try following code but I fail. Why?
------------------------------
string2 s = static_cast<string2> ( string("this is a test") );
string2 t = (string2) string("this is a test");
------------------------------
when I use 2, commented now, lines in class string2 it works.
Other question: This is correct or I can write it better?
-----------------------------
// vector<string> v;
string temp = string2(v.back()).trim();
-----------------------------
I know, conversion from string2 to string is very easy (& quick) but
conversion from string to string2 with this constructor of string2 is quick?
Maybe it copy whole string to string2 with memory copy operation? (slow)
Thanks for your time
------------------------------
class string2 : public string
{
public:
//string2() {}
//string2(string s) { *this = s; }
string trim()
{
int st = this->find_first_not_of(" \t\r\n");
int ed = this->find_last_not_of(" \t\r\n");
if (st != npos) return this->substr(st, ed - st + 1); else return string();
}
};
------------------------------
As you see, I have no changes in class data (extra variables, virtual functions etc)
I try following code but I fail. Why?
------------------------------
string2 s = static_cast<string2> ( string("this is a test") );
string2 t = (string2) string("this is a test");
------------------------------
when I use 2, commented now, lines in class string2 it works.
Other question: This is correct or I can write it better?
-----------------------------
// vector<string> v;
string temp = string2(v.back()).trim();
-----------------------------
I know, conversion from string2 to string is very easy (& quick) but
conversion from string to string2 with this constructor of string2 is quick?
Maybe it copy whole string to string2 with memory copy operation? (slow)
Thanks for your time