所以,我正在玩C指针和指针算术,因为我对它们不太满意.我想出了这段代码.
char* a[5] = { "Hi", "My", "Name", "Is" , "Dennis"};
char** aPtr = a; // This is acceptable because 'a' is double pointer
char*** aPtr2 = &aPtr; // This is also acceptable because they are triple pointers
//char ***aPtr2 = &a // This is not acceptable according to gcc 4.8.3, why ?
//This is the rest of the code, the side notes are only for checking
printf("%s\n",a[0]); //Prints Hi
printf("%s\n",a[1]); //Prints My
printf("%s\n",a[2]); //Prints Name
printf("%s\n",a[3]); //Prints Is
printf("%s\n",a[4]); …Run Code Online (Sandbox Code Playgroud) 我正在研究virtual关键字在C++中的效果,我想出了这段代码.
#include<iostream>
using namespace std;
class A {
public:
virtual void show(){
cout << "A \n";
}
};
class B : public A {
public:
void show(){
cout << "B \n";
}
};
class C : public B {
public:
void show(){
cout << "C \n";
}
};
int main(){
A *ab = new B;
A *ac = new C;
B *bc = new C;
ab->show();
ac->show();
bc->show();
}
Run Code Online (Sandbox Code Playgroud)
预期的产出是:
B
C
B
Run Code Online (Sandbox Code Playgroud)
因为showB中的函数是非虚拟的.但是编译它的结果是:
B
C …Run Code Online (Sandbox Code Playgroud)