Cocos2d-x on the strange problem of conversion between std::string and const char*

Source: Internet
Author: User
Tags addchild
First, a string type data
    Ccuserdefault::shareduserdefault ()->setstringforkey ("Jason", "Hello") is stored;
    Ccuserdefault::shareduserdefault ()->flush ();
    
    Then get
    the string const char* FileName = Ccuserdefault::shareduserdefault ()->getstringforkey ("Jason") by the following way. C_STR () ;
    The print statement is normal and the output is Hello
    cclog ("filename:%s", fileName);
    But the following statement appears on the device on the error, or nothing, or the string is incomplete, or display Helvetica
    cclabelttf* label = cclabelttf::create (FileName, "Helvetica", );
    Label->setposition (CCP ());
    AddChild (label);


Then to solve the above problem, we change the way 
    cclabelttf* label = Cclabelttf::create (Ccuserdefault::shareduserdefault () Getstringforkey ("Jason"). C_str (), "Helvetica", "(");
    Label->setposition (CCP ());
    AddChild (label); The result is Hello, normal

Let's put it another way 
    std::string fileName = Ccuserdefault::shareduserdefault ()->getstringforkey ("Jason");
    cclabelttf* label = Cclabelttf::create (Filename.c_str (), "Helvetica", +);
    Label->setposition (CCP (240, 160)); The results are also Hello, normal


Another way to do it 
    std::string fileName = Ccuserdefault::shareduserdefault ()->getstringforkey ("Jason");
    Const char* file = Filename.c_str ();
    cclabelttf* label = cclabelttf::create (file, "Helvetica",);
    Label->setposition (CCP ());
    AddChild (label);
    As a result, the writing is also normal, then why the most up-to-do not get the correct results?

Summary: Although the underlying reasons do not know, but you can first remember: return std::string type is used std::string type to connect

My cocpos2d-x version cocos2d-2.1rc0-x-2.1.2, there are children's shoes to know why please enlighten.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.