我正在尝试创建一个类,在该类中我将指针声明为私有(int *intTest_pointeur
)。在创建类的过程中,我声明了该指针的值和地址。但是,当我回想起ClassPrintOut()
时,地址与我初始化的内容匹配,但与值不匹配。我的代码如下...
main.cpp:
#include <iostream>
#include "Class1.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
Class1 *test(0);
test = new Class1();
test->ClassPrintOut();
delete test;
return 0;
}
Class1.h:
#ifndef CLASS1_H_INCLUDED
#define CLASS1_H_INCLUDED
#include <vector>
#include <iostream>
class Class1
{
public:
Class1();
~Class1();
void ClassPrintOut() const;
private:
int *intTest_pointeur;
};
#endif
Class1.cpp:
#include "Class1.h"
using namespace std;
Class1::Class1()
{
intTest_pointeur=0;
int age(10);
intTest_pointeur=&age;
cout <<"address Vector1: "<<intTest_pointeur << " - value1:" << *intTest_pointeur << endl;
}
Class1::~Class1()
{
}
void Class1::ClassPrintOut() const
{
cout <<"address Vector2: "<<intTest_pointeur << " - value2:" << *intTest_pointeur << endl;
}
结果是:
Hello world!
address Vector1: 0x6dfe6c - value1:10
address Vector2: 0x6dfe6c - value2:7672216
我不知道这个值2的来源!