共用方式為


編譯器警告 (層級 4) C4840

類別 ' type ' 的非可攜式使用做為 variadic 函式的引數

備註

傳遞至 variadic 函式的類別或結構必須簡單複製。 傳遞這類物件時,編譯器只會進行位元複製,而且不會呼叫建構函式或解構函式。

此警告從 Visual Studio 2017 開始提供。

範例

下列範例會產生 C4840,並示範如何修正此問題:

// C4840.cpp
// compile by using: cl /EHsc /W4 C4840.cpp
#include <stdio.h>

int main()
{
    struct S {
        S(int i) : i(i) {}
        S(const S& other) : i(other.i) {}
        operator int() { return i; }
    private:
        int i;
    } s(0);

    printf("%i\n", s); // warning C4840 : non-portable use of class 'main::S'
                       // as an argument to a variadic function
    // To correct the error, you can perform a static cast
    // to convert the object before passing it:
    printf("%i\n", static_cast<int>(s));
}

針對使用 CStringW 建置和管理的字串,提供的 operator LPCWSTR() 應該用來將 物件轉換成 CStringW 格式字串所預期的 C 樣式字串指標:

    CStringW str1;
    CStringW str2;
    // ...
    str1.Format("%s", static_cast<LPCWSTR>(str2));