const

    3熱度

    3回答

    在C++中,標記爲static的成員由給定類的所有實例共享。無論是否爲私有都不會影響一個變量由多個實例共享的事實。如果有任何代碼會嘗試修改它,那麼在那裏有const會警告你。 如果這是嚴格的private,那麼類的每個實例都會得到它自己的版本(儘管有優化器)。這是我讀的here。我的問題是,爲什麼static const int而不是將所需的變量放在private?我知道每個對象都會有自己的,但爲

    6熱度

    1回答

    我有一個C結構是這樣的: struct my_struct { int i; double d; struct expensive_type * t; }; 這種結構的一個實例被創建和初始化爲: struct my_struct * my_new(int i , double d) { struct my_struct * s = malloc(si

    2熱度

    3回答

    我應該寫: template<class T> class Foo { typename const T* x; }; 或: template<class T> class Foo { const typename T* x; };

    2熱度

    4回答

    爲什麼const B和const A*不能區分,當B被定義爲A*?當編譯這個簡單的例子: struct A {}; typedef A* B; void f1(const A* a1); void f2(const B a2); int main() { const A a; f1(&a); f2(&a); } 我得到以下編譯器的輸出(G ++

    2熱度

    1回答

    我定義了一個類array_view和一個類strided_view(想想array_view和strided_array_view http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0122r0.pdf),我想專精我可以迭代效率的方式 讓我們來說說我有一個函數調度器,它嘗試專門處理不同的情況。 讓我們開始用一些簡單的代碼 templa

    0熱度

    1回答

    我有這個C文件 #include "pointer.h" int switcher(int * i) { int a = *i; switch (a) { case 1: return 0; default: return 1; } } 和相關的頭只有一行 int switcher(int * i); 這編譯使用

    3熱度

    1回答

    在下面的例子: file1.c中: #include <stdlib.h> #include <stdio.h> extern const char* szVers; extern const int iTest; extern const char cText[]; int main(int argc, char** argv) { printf("Result = %

    0熱度

    1回答

    裏面的iOS SDK,大量的蘋果定義的常量可以發現看起來像這樣: extern const CFStringRef kSomeReallyNiceConstant __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_8_0); 如果我檢查的這個常量標準的方式存在: if (NULL == &kSomeReallyNiceConstant)

    3熱度

    1回答

    我有類包含需要使用函數初始化的常量字段。使用類的靜態方法在構造函數的初始化列表中初始化這些值是否合適? 我還沒有遇到過這樣的問題,但是當我讀到'靜態初始化失敗'時,我擔心我會忽略一些稍後會回來咬我的東西,寧可養成正確初始化的習慣。 例子: square.hpp: class Square { const double area; static initArea(double

    3熱度

    3回答

    那麼我沒有意識到const可能會像指針一樣令人困惑。有人可以請步驟解釋下面的代碼在const方面做了什麼? const int*const Method3(const int*const&)const; 即使對於非新手程序員也是如此令人困惑。