I have a class function which is receving a BSTR. In my class I have a member variable which is LPCSTR. Now I need to append BSTR ins LPCSTR. How I can do that. Here is my function.
void MyClass::MyFunction(BSTR text)
{
LPCSTR name = "Name: ";
m_classMember = name + text; // m_classMember is LPCSTR.
}
in my m_classMember I want that after this function value should be "Name: text_received_in_function". How i can do that.
Use the Microsoft specific
_bstr_t
class, which handles the ANSI/Unicode natively. Something likeis what you almost want. However, as pointed out by the remarks, you have to manage the lifetime of
m_classMember
and the concatened string. In the example above, the code is likely to crash.If you own the
MyClass
object, you could simply add another member variable:and then use
m_classMember
as a pointer to the string content ofm_concatened
.Otherwise, prior to the assignment of
m_classMember
, you should free it in the same way you allocated it (free
,delete []
, etc), and create a newchar*
array in which you copy the content of the concatened string. Something likeshould do the work.