Home > Back-end >  Novice, please issue of null Pointers
Novice, please issue of null Pointers

Time:09-23

I am a freshman just learning c + + soon wanted to do a small game with cocos2dx headphones
Today when doing the virtual joystick to rocker class member function to get a handle on the contact direction deflection of the coordinates of the incremental
But in running mistakes in the code shown in figure 1
Debugging shows that this is a null pointer
Immediately put my class and pointer based serious rickety problems exposed..
Here would like to ask you a few questions I haven't understand
1. The corresponding to the different object of the class member variables with the same why should not the same as shown in figure 2 rockerBGR the member variables in the code in figure 3 has the same value?
2. Null Pointers can only reference about class member function cannot problems involving a member variable I learned about in baidu, so if I want to get these in figure 1 member variables (such as code 2 rockerBGR have determined) and through the member function returns a vector what should I do?
There were gaps in beginner c + + base may organization of language and question some neither fish nor fowl, please forgive me
If there is anything I didn't understand the problem where the welcome message I explain it again
First time post   thank you first!!

CodePudding user response:


I more than a decade ago at c/c + + first year is dizzy, the second year to awake, in addition to catch up with learning practice, there is no other shortcut,

CodePudding user response:

Top of a wave for help!!

CodePudding user response:

Make of I also want to write the game, what do you want to do,

Don't understand

CodePudding user response:

The
reference reply: 3/f thousand dream life
I also want to write some of the game, you want to do

Don't understand


Where there is a mistake? I can't see nan? Error in theory there should be a red line ah what right

CodePudding user response:

But in running mistakes in the code shown in figure 1
==& gt;
Figure 1 inside have no direct call to the figure 2, figure 3 code, so you need to see the figure 2 and figure 3 two functions specific what is called,


1. The corresponding to the different object of the class member variables should be of the same name is not the same.
==& gt;
This depends:
If two objects HRocker A, B;
HRocker member variables of CustomColor * color;

When object initialization:
The same situation:
Color=new CustomColor CustomColor * (11, 22, 12);
A=HRocker (color);
B=HRocker (color);


Different conditions:
CustomColor * color1=new CustomColor (11, 22, 12);
CustomColor * color2=new CustomColor (11, 22, 12);
A=HRocker (color1);
B=HRocker (color2);

Note: the color1 and color2 just memory address is different, but the content of the object is the same, so when asked if the two objects as you want to points clear memory address and specific content,
In c + + object-oriented world, anything in need an area to show yourself, so must first find a place (application memory), and then on the site show specific things (objects),

CodePudding user response:

 
Class A
{
int x;
Int getX ()
{
return x;
}
}

The main ()
{
A * A=null;
A - & gt; GetX ();//must be an error, you just say the null pointer if forced to use a class member function, the function that a member variable cannot be used, otherwise don't normal, and it is likely to have an error,
}

CodePudding user response:

The
reference response: 6th floor thousand dream life
 
Class A
{
int x;
Int getX ()
{
return x;
}
}

The main ()
{
A * A=null;
A - & gt; GetX ();//must be an error, you just say the null pointer if forced to use a class member function, the function that a member variable cannot be used, otherwise don't normal, and it is likely to have an error,
}


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
As the code above a - & gt; GetX ();
Just like the following pseudo code:
GetX (a [as] this pointer, other parameters... )

So when a=null [0]

The equivalent of
GetX ()
{
[0 x00000000 here this pointer to the memory...
[in this access member variables, such as x, is equivalent to, in this (0 x00000...) as a starting point, the location of the byte offset so-and-so get x]
[what do you think you will need the x in memory starting off? Surely not, so the operating member variables of these member variables must not normal program]
}

CodePudding user response:

reference 5 floor OODeveloper reply:
but running mistakes in the code shown in figure 1
==& gt;
Figure 1 inside have no direct call to the figure 2, figure 3 code, so you need to see the figure 2 and figure 3 two functions specific what is called,


1. The corresponding to the different object of the class member variables should be of the same name is not the same.
==& gt;
This depends:
If two objects HRocker A, B;
HRocker member variables of CustomColor * color;

When object initialization:
The same situation:
Color=new CustomColor CustomColor * (11, 22, 12);
A=HRocker (color);
B=HRocker (color);


Different conditions:
CustomColor * color1=new CustomColor (11, 22, 12);
CustomColor * color2=new CustomColor (11, 22, 12);
A=HRocker (color1);
B=HRocker (color2);

Note: the color1 and color2 just memory address is different, but the content of the object is the same, so when asked if the two objects as you want to points clear memory address and specific content,
In c + + object-oriented world, anything in need an area to show yourself, so must first find a place (application memory), and then on the site show specific things (objects),

Thank you for your patience to explain!!!! I have understood null Pointers can't really call member variables most does not involve a member of the member variable function called
The figure 3, figure 2 members can use the same variable values should be OnTouchMoved this binding to monitor events function to provide them with the same pointer (guess because reference figure 2 functions of the function of binding to monitor the operation of the event)
Thank you again!

CodePudding user response:

reference response: 7th floor thousand dream life
Quote: refer to the sixth floor response: thousand dreams life

 
Class A
{
int x;
Int getX ()
{
return x;
}
}

The main ()
{
A * A=null;
A - & gt; GetX ();//must be an error, you just say the null pointer if forced to use a class member function, the function that a member variable cannot be used, otherwise don't normal, and it is likely to have an error,
}


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
As the code above a - & gt; GetX ();
Just like the following pseudo code:
GetX (a [as] this pointer, other parameters... )

So when a=null [0]

The equivalent of
GetX ()
{
[0 x00000000 here this pointer to the memory...
[in this access member variables, such as x, is equivalent to, in this (0 x00000...) as a starting point, the location of the byte offset so-and-so get x]
[what do you think you will need the x in memory starting off? Surely not, so the operating member variables of these member variables must not normal program]
}

Questions about the null pointer I have understood the
But also meet the new packaging into the problem of the android platform haha
Games should be quite interesting but I think to choose an ecologically healthier engine is good brother come on!!