假设在我的 main 方法中,我声明了一个指向在堆上创建的数组的 const int 数组指针。然后我想在构造函数 TryInitialize() 中初始化它的值(使用内存地址),然后将它们打印出来。这不起作用,我想知道我做错了什么?谢谢!
#include "stdafx.h"
#include "part_one.h"
#include <string>
#include <iostream>
using namespace std;
string createTable(unsigned int* acc, double* bal, int n) {
string s;
char buf[50];
for (int i = 0; i < n; i++) {
sprintf_s(buf,"%7u\t%10.2f\n",acc[i], bal[i]);
s += string(buf);
}
return s;
}
int _tmain(int argc, _TCHAR* argv[])
{
const int *tempInt = new const int[4];
TryInitialize(tempInt);
std::cout << tempInt[1] << endl;
system("pause");
return 0;
}
这是我的构造函数的代码:
#include "part_one.h"
TryInitialize::TryInitialize(void) {
}
TryInitialize::TryInitialize(int constInt[]) {
constInt[0] = 8;
constInt[1] = 0;
constInt[2] = 0;
constInt[3] = 8;
}