[問題] G++ and VC++ Constructor 問題

看板C_and_CPP (C/C++)作者 (ROGZ)時間7年前 (2017/06/01 21:04), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
開發平台(Platform): (Ex: Win10, Linux, ...) Win10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) G++,VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 下面的程式碼在VC++可以編譯,但在g++就會出現問題,g++會 用const char*去做construct,但為什麼VC++就不會出現錯誤? 找一下網路,有人用下面這樣就可以了 A(const char* s) { x = s; } 但我想問一下,string直接給char*,為什麼string 可以抓到正確的陣列位數? 以及這樣寫會有問題嗎? 謝謝!! class A { private: string x; public: A(string s) { x = s; } }; int main() { A a = "123"; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.118.150.41 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1496322242.A.BC5.html
文章代碼(AID): #1PC132l5 (C_and_CPP)
文章代碼(AID): #1PC132l5 (C_and_CPP)