I have the following code
#include <iostream>
#include <typeinfo>
using namespace std;
class myClass{
public:
myClass(){
cout<<"constructor called..."<<endl; }
};
int main(){
myClass ** p1;
myClass *p2[5];
*(p2+4) = new myClass;
*p1 = new myClass; // "constructor called..." printed, but segmentation fault
cout<<typeid(p1).name()<<endl;
// "PP7myClass" printed, after commenting out *p1 = new myClass;
// what is PP7?
cout<<typeid(2).name()<<endl;
// "A5_P7myClass" printed, after commenting out *p1 = new myClass;
// what is A5_P7?
if(typeid(p1)==typeid(p2)) cout<<"==="<<endl;
if(typeid(p1)==typeid(*p2)) cout<<"&&&"<<endl;
// I expected at least one of the above cout
// two lines should be printed, but nothing printed actually, why?
return 0;
}
- why there is segmentation fault after calling the constructor for
p1
? - If the line
*p1 = new myClass;
is commented out, "PP7myClass" and "A5_P7myClass" printed, what are "PP7" and "A5_P7"? - If I define a function
void func(myClass a, myClass b){}
and then dofunc(p1, p2);
, the compiler will complain that can not convertmyClass **
tomyClass
for two arguments, so that meansp1
andp2
are both of typemyClass **
, but why for the two lines abovereturn 0;
, nothing is printed?