#include <iostream> using namespace std; class A { public: A() : m_ival(0) { test(); } virtual void func() { cout << m_ival << endl; } void test() { func(); } public: int m_ival; }; class B : public A { public: B() { test(); } void func() { ++ m_ival; cout << m_ival << endl; } }; int main() { A* p = new B; p->test(); delete p; return 0; }
问 1:分析下面的程序,给出运行结果
输出:ios
0 1 2
问 二 :在子类与父类构造函数中都调用test(), 那么编译器是怎么肯定应该调用的哪一个 func() 函数版本呢?
相关概念:面试
发生了什么?算法
在构造 A 部分时:函数
在构造 B 部分时:学习
总结:spa
问 三:上述的代码是否有其它问题?
问题 2指针
要点
HR 问:编写代码将一个链表反转打印
面试者:须要思考的问题code
struct Node { int v; struct Node* next; };
void reversse_display_list(struct Node* list) { if( list ) { reversse_display_list(list->next); printf("%d ", list->v); } }
void reversse_display_list_stack(struct Node* list) { if( list ) { LinkStack<int> stack; while( list ) // O(n) { stack.push(list->v); list = list->next; } while( stack.size() > 0 ) // O(n) { printf("%d ", stack.top()); stack.pop(); } printf("\n"); } }
struct Node* reverse_list(struct Node* list) // O(n) { if( list ) { struct Node* guard = NULL; struct Node* next = list->next; while( next ) { list->next = guard; guard = list; list = next; next = list->next; } list->next = guard; } return list; }
要点
不用加减乘除运算符作整数加法
面试者: 须要思考的问题对象
int add_1(int a, int b) { int ret = 0; asm volatile ( "movl %%eax, %%ecx\n" "addl %%ebx, %%ecx\n" : "=c"(ret) : "a"(a), "b"(b) ); return ret; } int add_2(int a, int b) { int part = 0; int carry = 0; do { part = a ^ b; carry = (a & b) << 1; a = part; b = carry; } while( b != 0 ); return a; }
要点