指针将随机打印它的地址,而不是它的指针值,C ++ [英] Pointer will randomly print it's address instead of it's pointed value, C++
本文介绍了指针将随机打印它的地址,而不是它的指针值,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有这个问题,其中的输出打印我的指针的地址,我不知道为什么这会发生cuz的指针是不会修改
So I have this problem where the output prints the address of my pointer, I have no idea why this happens cuz the pointers is not modified at all
代码:
using namespace std;
class AndroideAbstracto {
protected:
int *vida;
int *fuerza;
int *velocidad;
public:
void setvalores(int vi, int fu, int ve) {
velocidad = &ve;
vida = &vi;
fuerza = &fu;
};
virtual void imprimir(void) = 0;
};
class Androide : public AndroideAbstracto {
public:
void imprimir() {
std::cout << "Caracteristicas del androide:" << endl;
cout << "Velocidad = " << *velocidad << endl;
cout << "Vida = " << *vida << endl;
cout << "Fuerza = " << *fuerza << endl;
};
};
class Decorator : public AndroideAbstracto {
protected:
AndroideAbstracto *AndroideDec;
public:
Decorator(AndroideAbstracto* android_abs) {
AndroideDec = android_abs;
}
virtual void imprimir(void) = 0;
};
class Androide_Con_Habi : public Decorator {
protected:
string habilidad;
public:
Androide_Con_Habi(AndroideAbstracto* android_abs, string habi) : Decorator(android_abs) {
habilidad = habi;
}
virtual void imprimir() {
AndroideDec->imprimir();
cout << "La habilidad especial del androide es: " << habilidad << endl;
}
};
class Androide_Elegido : public Decorator {
protected:
bool elegido;
public:
Androide_Elegido(AndroideAbstracto *android_abs, bool es) : Decorator(android_abs) {
elegido = es;
}
virtual void imprimir() {
if (elegido) {
// *vida =(*vida) * 2; //Im quite new to C++ so im not really
// *fuerza *=2; //sure how should I multiply these pointers
// *velocidad *=2;
// AndroideDec->setvalores(vida*2,fuerza*2,velocidad*2);
AndroideDec->imprimir();
cout << "Este androide es uno de los elegidos";
}
}
};
int main(int argc, char *argv[]) {
Androide *andro = new Androide();
andro->setvalores(600, 700, 300);
andro->imprimir();
Androide_Con_Habi *andro_con_habi = new Androide_Con_Habi(andro, "Volar");
andro_con_habi->imprimir();
Androide_Elegido *superpoderoso = new Androide_Elegido(andro, true);
superpoderoso->imprimir();
delete superpoderoso;
delete andro;
delete andro_con_habi;
return 0;
}
我不知道为什么,但打印:
I have no idea why but this prints:
Caracteristicas del androide:
Velocidad = 300
Vida = 600
Fuerza = 700
Caracteristicas del androide:
Velocidad = 300
Vida = 152436744
Fuerza = -1074718788
La habilidad especial del androide es: Volar
Caracteristicas del androide:
Velocidad = 300
Vida = 152436744
Fuerza = 1
Este androide es uno de los elegidos
推荐答案
void setvalores(int vi, int fu, int ve) {
velocidad = &ve;
vida = &vi;
fuerza = &fu;
};
指向 vi
, fu
和 ve
在函数返回时无效。你没有看到地址被打印,而只是垃圾。
The pointers to vi
, fu
, and ve
get invalidated when the function returns. You're not seeing addresses being printed, but simply garbage.
你的整个设计不,也不应该使用指针。
Your entire design doesn't and shouldn't need to use pointers though.
这篇关于指针将随机打印它的地址,而不是它的指针值,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文