情况1:
[flydream@flydream A]$ cat A.cpp
#include <iostream>
using namespace std;
class Base
{
public:
void f2()
{
cout << "In Base F2" << endl;
}
void f1()
{
cout << "In Base F1" << endl;
}
};
class Der : public Base
{
public:
void f2()
{
cout << "In Der F2" << endl;
}
void f1()
{
cout << "In Der F1" << endl;
}
};
int main(int argc, char **argv)
{
Der *pder = NULL; // right why ? Base b; Der *pder = (Der *)&b; right ? 为什么这里随便写个都是正确的呢[/color]
pder->f1();
pder->f2();
return 0;
}
[flydream@flydream A]$ g++ -g -Wall A.cpp -o A
[flydream@flydream A]$ ./A
In Der F1
In Der F2
[flydream@flydream A]$
情况二:
[flydream@flydream A]$ cat B.cpp
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f2()
{
cout << "In Base F2" << endl;
}
void f1()
{
cout << "In Base F1" << endl;
}
};
class Der : public Base
{
public:
void f2()
{
cout << "In Der F2" << endl;
}
void f1()
{
cout << "In Der F1" << endl;
}
};
int main(int argc, char **argv)
{
Base b;
Der *pder = (Der *)&b;
pder->f1();
pder->f2(); // 这一个的输出不是很理解
return 0;
}
[flydream@flydream A]$ g++ -g -Wall B.cpp -o B
[flydream@flydream A]$ ./B
In Der F1
In Base F2
[flydream@flydream A]$
情况3:
[flydream@flydream A]$ cat C.cpp
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f2()
{
cout << "In Base F2" << endl;
}
void f1()
{
cout << "In Base F1" << endl;
}
};
class Der : public Base
{
public:
void f2()
{
cout << "In Der F2" << endl;
}
void f1()
{
cout << "In Der F1" << endl;
}
};
int main(int argc, char **argv)
{
Der *pder = NULL;
pder->f1();
pder->f2();
return 0;
}
[flydream@flydream A]$ g++ -g -Wall C.cpp -o C
[flydream@flydream A]$ ./C
In Der F1
Segmentation fault (core dumped)
[flydream@flydream A]$
[flydream@flydream A]$ cat A.cpp
#include <iostream>
using namespace std;
class Base
{
public:
void f2()
{
cout << "In Base F2" << endl;
}
void f1()
{
cout << "In Base F1" << endl;
}
};
class Der : public Base
{
public:
void f2()
{
cout << "In Der F2" << endl;
}
void f1()
{
cout << "In Der F1" << endl;
}
};
int main(int argc, char **argv)
{
Der *pder = NULL; // right why ? Base b; Der *pder = (Der *)&b; right ? 为什么这里随便写个都是正确的呢[/color]
pder->f1();
pder->f2();
return 0;
}
[flydream@flydream A]$ g++ -g -Wall A.cpp -o A
[flydream@flydream A]$ ./A
In Der F1
In Der F2
[flydream@flydream A]$
情况二:
[flydream@flydream A]$ cat B.cpp
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f2()
{
cout << "In Base F2" << endl;
}
void f1()
{
cout << "In Base F1" << endl;
}
};
class Der : public Base
{
public:
void f2()
{
cout << "In Der F2" << endl;
}
void f1()
{
cout << "In Der F1" << endl;
}
};
int main(int argc, char **argv)
{
Base b;
Der *pder = (Der *)&b;
pder->f1();
pder->f2(); // 这一个的输出不是很理解
return 0;
}
[flydream@flydream A]$ g++ -g -Wall B.cpp -o B
[flydream@flydream A]$ ./B
In Der F1
In Base F2
[flydream@flydream A]$
情况3:
[flydream@flydream A]$ cat C.cpp
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f2()
{
cout << "In Base F2" << endl;
}
void f1()
{
cout << "In Base F1" << endl;
}
};
class Der : public Base
{
public:
void f2()
{
cout << "In Der F2" << endl;
}
void f1()
{
cout << "In Der F1" << endl;
}
};
int main(int argc, char **argv)
{
Der *pder = NULL;
pder->f1();
pder->f2();
return 0;
}
[flydream@flydream A]$ g++ -g -Wall C.cpp -o C
[flydream@flydream A]$ ./C
In Der F1
Segmentation fault (core dumped)
[flydream@flydream A]$