zoukankan      html  css  js  c++  java
  • C++的string类的c_str()方法和C的字符指针的关系具体见 C++ primer第5版第111页

    // Beginning Game Programming
    // Chapter 2 - WindowTest program

    #include <windows.h>
    #include <iostream>
    using namespace std;
    const string WindowsClassName = "Hello Windows";
    const string TitleBarName = "The Hello Program";

    // The window event callback function
    LRESULT CALLBACK BigBadGameWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    RECT drawRect;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_PAINT:
    {
    hdc = BeginPaint(hWnd, &ps); //start drawing
    for (int n = 0; n < 20; n++)
    {
    int x = n * 20;
    int y = n * 20;
    drawRect = { x, y, x+200, y+20 };
    DrawText(hdc, TitleBarName.c_str(), TitleBarName.length(), &drawRect, DT_CENTER);
    }
    EndPaint(hWnd, &ps); //stop drawing
    }
    break;

    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
    }

    // Helper function to set up the window properties
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    //set the new window's properties
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)BigBadGameWindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = WindowsClassName.c_str(); //C++标准库中 string类的变量有一个成员方法c_str(),功能是生成一个c语言的const char*指针,指向以空字符终止的数组。因为在win SDK编程中,这个wc.lpszClassNmae只能接受char*的指针变量,因此必须把string变量转化为char* 类型的指针变量,参考帖子https://www.cnblogs.com/cyx-b/p/12411673.html
    //C++的string类的c_str()方法和C的字符指针的关系 具体见 C++ primer第5版 第111页
    wc.hIconSm = NULL;
    return RegisterClassEx(&wc);
    }


    // Helper function to create the window and refresh it
    bool InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
    //create a new window
    HWND hWnd = CreateWindow(
    WindowsClassName.c_str(), //window class name
    TitleBarName.c_str(), //title bar name, 也就是windows caption
    WS_OVERLAPPEDWINDOW, //window style
    CW_USEDEFAULT, CW_USEDEFAULT, //position of window
    640, 480, //dimensions of the window
    NULL, //parent window (not used)
    NULL, //menu (not used)
    hInstance, //application instance
    NULL); //window parameters (not used)

    //was there an error creating the window?
    if (hWnd == 0) return 0;

    //display the window
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return 1;
    }

    // Entry point for a Windows program
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
    {
    //create the window
    MyRegisterClass(hInstance);
    if (!InitInstance(hInstance, nCmdShow)) return 0;

    // main message loop
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }

    ======================================

    另一个帖子中的讲解 http://www.zzvips.com/article/126359.html

    c_str()就是把string类对象转换成和c兼容的char *类型。这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式

    const char *c_str();
    c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.

    c_str()就是把string类对象转换成和c兼容的char *类型。
    这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
    注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
    比如:最好不要这样:
    char* c;
    string s="1234";
    c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理

    应该这样用:
    char c[20];
    string s="1234";
    strcpy(c,s.c_str());
    这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作

    再举个例子
    c_str() 以 char* 形式传回 string 内含字符串
    如果一个函数要求char*参数,可以使用c_str()方法:
    string s = "Hello World!";
    printf("%s", s.c_str()); //输出 "Hello World!"

  • 相关阅读:
    javascript中的时间控制函数
    javascript在事件监听方面的兼容性总结
    javascript程序库比较(二):事件处理
    又要开始了
    ASP.NET怎么防止多次点击提交按钮重复提交
    asp.net中ashx文件如何调用session
    Session超时和丢失,如何让Sessioon永不过期
    组件
    csss
    C# JSONHelper之Json转换方法大全
  • 原文地址:https://www.cnblogs.com/Thermo/p/15766862.html
Copyright © 2011-2022 走看看