>and it confused me much, what cause it be ???
Your question confuses me much, what is the problem? Every time showX is called, x is set to 0, printed, incremented, and then printed again. Aside from your ghastly use of undefined behavior, this is correct and predictable.
Code:
#include <iostream>
#include <string>
class character
{
public:
void showX();
private:
int x;
};
void character::showX()
{
x=0;
/* Note that I had to change calls to cout,
** cin, and endl to include the namespace prefix
** since you don't have a using statement. I
** also changed the first call to cout so that it
** ends with a space to create the exact output
** from your post.
*/
std::cout << x << ' ';
x++;
std::cout << x << std::endl;
}
// void main is wrong, main returns an int.
int main()
{
character black;
black.showX();
black.showX();
}
--------
Output:
0 1
0 1
Could you be more specific as to what output you were expecting?
-Prelude