Re: Funny thing about glib::ustring::substr(i, n)



Maybe it is gtkmm-2.2 specific, I don't know.

What I exactly did is, I bound a Glib::ustring as a parameter to an event handler
and a couple of events start with the same prefix so I wanted to sort them out
by the first part of the string.

void onMenuEvent(Glib::ustring param)
{
  if (param.substr(0,4) == "file")
  {
    /* ... */
  }
  else
  {
    /* ... */
  }
}

This one really does not work. And the test case below doesn't work for me,
too. I tried it, I didn't just think it up.

What works, is the following, so I don't think it's the ==-operator:

void onMenuEvent(Glib::ustring param)
{
  if (param == "file open")
  {
    /* ... */
  }
  else if (param == "file save")
  {
    /* ... */
  }
  else
  {
    /* ... */
  }
}

>Daniel J. Lauk wrote:
>
>>Hi List.
>>
>>In the following code the if branch never gets entered:
>>
>>void somefunc(void)
>>{
>>  Glib::ustring str = "This is some text";
>>  if ( str.substr(0,4) == "This" )
>>  {
>>    std::cout << "if branch entered" << std::endl;
>>  }
>>  else
>>  {
>>    std::cout << "else branch entered" << std::endl;
>>  }
>>}
>>
>>If you replace Glib::ustring with std::string the if branch will be executed.

>>
>>
>>Is this a bug or am I confusing something?
>>
>>Just for the files: I'm using gtkmm-2.2.12 on MSVC running upon a GTK+-2.4.0

>>(without any other known problems).
>>
>This works in my program without any problems. I used g++-3.3.3 and 
>gtkmm-2.4.5.
>
>Regards
>
>Jaroslaw Mroczek
>
>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]