CString Vs std::string
-
Hi !! There is a
AllocSysString
member in CString class. I want to give the same functionality to the string represented by std::string. Is it possible ??? -
Hi !! There is a
AllocSysString
member in CString class. I want to give the same functionality to the string represented by std::string. Is it possible ???AllocSysString is used for BSTR's so just off the top of my head...
#include <comdef.h>
class MyString {
public:
std::string& get() {
return m_str;
}operator BSTR () {
_bstr_t tmp;
tmp = m_str.c_str();
return tmp.copy();
}
protected:
std::string m_str;
};this is pretty crude, but may give you some ideas... ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
-
AllocSysString is used for BSTR's so just off the top of my head...
#include <comdef.h>
class MyString {
public:
std::string& get() {
return m_str;
}operator BSTR () {
_bstr_t tmp;
tmp = m_str.c_str();
return tmp.copy();
}
protected:
std::string m_str;
};this is pretty crude, but may give you some ideas... ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
Thanks for your response. Can you please also tell me that how can i use the operator BSTR of your class. Example if i have string std::string strName = "Jim"; BSTR bstrName; Now please tell me how i use your class to copy my std::string in BSTR. Thanks.
-
Thanks for your response. Can you please also tell me that how can i use the operator BSTR of your class. Example if i have string std::string strName = "Jim"; BSTR bstrName; Now please tell me how i use your class to copy my std::string in BSTR. Thanks.
std::string strName = "Jim";
BSTR bstrName;
MyString s;
s.get() = strName;bstrName = s;
Thats it. But before you go using it, you should definitely read up on C++ operator overloads and conversion operators. COnversion operators are what allow you to do what we did above in the line: bstrName = s; Basically the compiler sees you have a conversion operator for the type of the assignment (in our case a BSTR) and then calls it for you. ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!