Home > Back-end >  VS2015, class study
VS2015, class study

Time:12-09

#include
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; //output is want results 10
Cout & lt; <"Area 1:" & lt; Cube c2.
Int * p2=c2. GetLWH (20, 30, 5);
Cout & lt; <"The area 2:" & lt; Cout & lt; //p1 here [0], is not 10, according to???????
Cout & lt; Bool ret=isSame (p1, p2);
Cout & lt; system("pause");
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:

refer to the second floor Spring_L response:
want p1 [0]=10; P1 [1]=20; P1 [2]=30;

 
The class cube
{
Public:
Int * getLWH (int l, int w, int h)
{
M_H=h;
M_L=l;
M_W=w;
M_arr [0]=m_L;
M_arr [1]=m_W;
M_arr [2]=m_H;
Return m_arr;//add
}
Private:
Int m_L;
Int m_W.
Int m_H;
Int m_arr [3].
} :

CodePudding user response:

Thank you thank you, look at your code an Epiphany! Has been successfully!