Junaid Siddiqui
Junaid Siddiqui

Reputation: 41

Accessing the elements using [ ] operator on a pointer to wstring

I am unable to use to [] operator on a pointer to a wstring object before converting it to const reference using c_str() function.

In my program getwString() returns a pointer to wstring

This is working:

`if(obj->getwString()->c_str()[5]!='H')`

This is not:

`if(obj->getwString()->[5]!='H')`

How can I dereference it instead of using c_str() function Need Help

Upvotes: 0

Views: 146

Answers (2)

dragosht
dragosht

Reputation: 3275

If you really want to access it that way you can do it like this:

if(obj->getwString()-> operator [](5) != 'H') , but this is not very pretty.

Upvotes: 1

nikolas
nikolas

Reputation: 8975

There is another dereference operator besides -> for cases like this one, the * operator:

std::wstring * ptr = obj->getwString();
(*ptr)[5];

or simply

(*obj->getwString())[5];

Upvotes: 1

Related Questions