M
Mug
hi, i have a strange problem on c++, can anyone explique to me:
here i have a class:
----------------------------------myclass.h--------------------
#ifndef MYCLASS_H
#define MYCLASS_H
#include <iostream>
class myclass
{
public:
myclass();
~myclass();
};
#endif
----------------------------myclass.cpp----------------------
#include "myclass.h"
myclass::myclass()
{
static bool cool=false;
if(!cool)
{
std::cout<<"hello world\n";
cool=true;
}
}
myclass::~myclass()
{
}
int main()
{
myclass *m1=new myclass();
myclass *m2=new myclass();
delete m1;
delete m2;
return 0;
}
the output of the program is quite intrigue me,
it print out just once "hello world", yet m1 and m2 are
two objects defferent, they are in defferent memory zone,for what
reason
they share the same static variable "static bool cool" ??
here i have a class:
----------------------------------myclass.h--------------------
#ifndef MYCLASS_H
#define MYCLASS_H
#include <iostream>
class myclass
{
public:
myclass();
~myclass();
};
#endif
----------------------------myclass.cpp----------------------
#include "myclass.h"
myclass::myclass()
{
static bool cool=false;
if(!cool)
{
std::cout<<"hello world\n";
cool=true;
}
}
myclass::~myclass()
{
}
int main()
{
myclass *m1=new myclass();
myclass *m2=new myclass();
delete m1;
delete m2;
return 0;
}
the output of the program is quite intrigue me,
it print out just once "hello world", yet m1 and m2 are
two objects defferent, they are in defferent memory zone,for what
reason
they share the same static variable "static bool cool" ??