我有一个有两个班级的课程。在fileTxt类中,我声明x& y值和相对指针。然后我去Byz课,我宣布delta x& y值总和为x& y先前声明的值。然后程序回到fileTxt类我打印新的x& y值。
1)为什么x&在dx / dy sum之前的Byz类内的y值与x&是声明?
2)为什么新的x& y值是声明的而不是x / y里面的byz + dx / dy值?
main.cpp中
#include "fileTxt.h"
#include <iostream>
using namespace std;
int main()
{
fileTxt _fileTxt;
_fileTxt.elab();
return 0;
}
fileTxt.cpp
#include "fileTxt.h"
#include "byz.h"
#include <iostream>
#include <string>
#include <cstdlib>
fileTxt::fileTxt()
{
}
fileTxt::~fileTxt()
{
delete[] pX;
pX=nullptr;
delete[] pY;
pY=nullptr;
}
void fileTxt::elab()
{
x=12.34;
y=56.78;
std::cout << "x declaration: " << *pX << std::endl;
std::cout << "y declaration: " << *pY << std::endl;
Byz _Byz;
_Byz.leggiByz();
std::cout << "\nnew x value: " << *pX << std::endl;
std::cout << "new y value: " << *pY << std::endl;
}
fileTxt.h
#ifndef FILETXT_H
#define FILETXT_H
#include <string>
class fileTxt
{
public:
fileTxt();
virtual ~fileTxt();
void elab();
private:
float x=0.0;
float y=0.0;
float *pX=&x;
float *pY=&y;
};
#endif // FILETXT_H
byz.cpp
#include "byz.h"
#include <string>
#include <iostream>
Byz::Byz()
{
}
Byz::~Byz()
{
}
void Byz::leggiByz()
{
dx=8.123;
dy=9.456;
std::cout << "\nx inside byz - before: " << *pX << std::endl;
std::cout << "y inside byz - before: " << *pY << std::endl;
std::cout << "\ndx: " << dx << std::endl;
std::cout << "dy: " << dy << std::endl;
*pX=*pX+dx;
*pY=*pY+dy;
std::cout << "\nx inside byz + dx: " << *pX << std::endl;
std::cout << "y inside byz + dy: " << *pY << std::endl;
}
byz.h
#ifndef BYZ_H
#define BYZ_H
#include <string>
class Byz
{
public:
Byz();
virtual ~Byz();
void leggiByz();
private:
float dx=0.0;
float dy=0.0;
float x=0.0;
float *pX=&x;
float y=0.0;
float *pY=&y;
};
#endif // BYZ_H
x声明:12.34
y声明:56.78
x里面byz - 之前:0.0
y by byz - 之前:0.0
dx:8.123
dy:9.456
x里面byz + dx:8.123
在里面byz + dy:9.456
新的x值:12.34
新y值:56.78