using namespace std; The class cube
{
Public:
//to get three high
Int * getLWH (int l, int w, int h)
{
M_H=h;
M_L=l;
M_W=w;
Int r []={m_L, m_W, m_H};
Return r;
}
//determine whether two cubes equal
Int calculateS ()
{
Int S=(m_W m_H + m_L m_L * * + m_H * m_W) * 2;
Return S;
}
Int calculateV () {
Int V=m_W m_L * * m_H; Return V.
}
Private:
Int m_L;
Int m_W.
Int m_H;
};
Int main ()
{
Cube c1;
Int * p1=c1. GetLWH (10, 20, 30);
Cout & lt;
Cout & lt; <"Area 1:" & lt;
Int * p2=c2. GetLWH (20, 30, 5);
Cout & lt; <"The area 2:" & lt;
Cout & lt;
Cout & lt;
Return (0);
}
Want to ask next at the first p1 [0]=10, while the second p1 is [0] 10, is this why? Thank you thank you
CodePudding user response:
Int r []={m_L, m_W, m_H};Return r;
Returns the pointer to a local variable (array),
Local variables the function scope is invalid,
If you want to be able to return to the r as a member variable,
CodePudding user response:
I just according to your ideas to try, (may be written in the right way, or didn't get the desired result)The class cube
{
Public:
Int * getLWH (int l, int w, int h)
{
M_H=h;
M_L=l;
M_W=w;
Return m_arr; //add
}
Private:
Int m_L;
Int m_W.
Int m_H;
Int m_arr [3]={m_L, m_W, m_H}; add
} :
In the main: cube c1;
Int * p1=c1. GetLWH (10, 20, 30);
Run shows are the same things, p1 to [0]=10; P1 [1]=20; P1 [2]=30;
CodePudding user response: