這幾天在學C++,對array
跟reference
的用法做個測試。
#include <iostream>
#include <string>
void test(std::string (&str)[2]){
std::cout << sizeof(str) << " " << sizeof(std::string);
}
int main(){
size_t n;
std::cin >> n;
size_t m = 2;
const size_t o = 3;
std::string *testStrArr1 = new std::string[n];// correct
std::string testStrArr2[n]; // wrong in VC++, correct in gcc, wrong in ISO C++
std::string testStrArr3[2] = {"hello","hi"}; // correct
std::string testStrArr4[m]; // wrong in VC++, correct in gcc, wrong in ISO C++
std::string testStrArr5[o]; // correct
test(testStrArr1); // wrong
test(testStrArr2); // wrong
test(testStrArr3); // correct
test(testStrArr4); // wrong
test(testStrArr5); // wrong [3]->[2]
}
正確的那行會印出:
8 4
No comments:
Post a Comment