Liny_@NotePad

沉迷ACG中

【20090821】C++培训日记-虚函数表

检测方法(VS2005):项目命令行加上参数/d1reportAllClassLayout,在编译时CTRL+F5搜索输出,查看类的对象布局。

vftable - 虚函数表; vbtable - 虚继承的父类表; member - 类的成员变量(这个只是写作方便说明 = =)。

总结:

  • 继承方式:非virtual继承:导入各个父类的结构(按照父类声明的顺序,从上到下),自身member在最后
    • 重写virtual方法:更新该方法最早定义的类的vftable
    • 新的virtual方法:在最左父类的vftable增加
  • 继承方式:有virtual继承:在自身member后增加virtual父类的结构(按照子类继承的顺序从左到右),同时在最前面增加vbtable(如果没有的话),增加一项指向父类结构
    • 重写virtual方法:更新该方法的最早定义的类的vftable
    • 新的virtual方法:在自身最前面增加vftable(如果没有的话),在自己的vftable增加

Swing:重置按钮的监听器实现

rt。。写了一个类实现ActionListener,actionPerformed时递归遍历容器中的所有组件 如果是可输入组件则清除内容,可选择组件则清除选择。

  1. import java.awt.Component;
  2. import java.awt.Container;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.JComboBox;
  7. import javax.swing.JList;
  8. import javax.swing.JTextArea;
  9. import javax.swing.JTextField;
  10.  
  11. /**
  12. * <p>Title: 重置按钮监听器</p>
  13. *
  14. * <p>Description: 单击重置按钮 清空窗体中数据</p>
  15. *
  16. * @author YOYO
  17. *
  18. * @create 2009-8-8
  19. *
  20. * @修改历史
  21. * <li>版本号 修改日期 修改人 修改说明
  22. * <li>
  23. * <li>
  24. */
  25. public class SuperResetButtonAction implements ActionListener {
  26.        
  27.         private Container container;
  28.        
  29.         public SuperResetButtonAction(Container container) {
  30.                 this.container = container;
  31.         }
  32.        
  33.         /**
  34.          * 清空容器内容
  35.          * @param parent
  36.          */
  37.         private void clear(Container parent) {
  38.                 for(Component component: parent.getComponents()){
  39.                         if(component instanceof Container) {
  40.                                 clear((Container) component);
  41.                         }
  42.                         if(component instanceof JTextField) {
  43.                                 ((JTextField) component).setText("");
  44.                         }
  45.                         if(component instanceof JTextArea) {
  46.                                 ((JTextArea) component).setText("");
  47.                         }
  48.                         if(component instanceof JComboBox) {
  49.                                 if(((JComboBox) component).getItemCount()>0) {
  50.                                         ((JComboBox) component).setSelectedIndex(0);
  51.                                 }
  52.                         }
  53.                         if(component instanceof JList) {
  54.                                 ((JList)component).clearSelection();
  55.                         }
  56.                 }
  57.         }
  58.  
  59.         public void actionPerformed(ActionEvent arg0) {
  60.                 clear(container);
  61.         }
  62.  
  63. }

C++中的this指针

 this总是指向当前对象,每次执行成员函数时,形如本例中的SetColor(int _color),总是会被编译成SetColor(CTestThis* this, int _color),编译器会自动带入对象地址作为this指针所指向的地址,本例中的全局函数也许可以加深对this指针的理解。

  1. #include <iostream>
  2.  
  3. class CTestThis
  4. {
  5. public:
  6.         int GetColor() { return this->color; }
  7.         void SetColor(int _color) { this->color = _color; }
  8.         void Display() { std::cout << this->color << std::endl; }
  9.  
  10. private:
  11.         int color;
  12. };
  13.  
  14. void SetColor(int _color, CTestThis* _)
  15. {
  16.         _->SetColor(_color);
  17. }
  18.  
  19. void Display(CTestThis* _)
  20. {
  21.         std::cout << _->GetColor() << std::endl;
  22. }
  23.  
  24. void main(void)
  25. {
  26.         CTestThis test;
  27.  
  28.         test.SetColor(3);
  29.         test.Display();
  30.  
  31.         SetColor(33, &test);
  32.         Display(&test);
  33.  
  34.         system("pause");
  35. }

VS2005 反汇编

右键工具栏->自定义->命令->调试,将反汇编的按钮拖到工具栏上,单步调试项目,单击该按钮即可查看汇编代码 = =

C++的继承与多态demo

继承(无虚函数):

  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. public:
  6.         void fun() { std::cout << "A::fun()" << std::endl; }
  7. };
  8.  
  9. class B: public A
  10. {
  11. public:
  12.         void fun() { std::cout << "B::fun()" << std::endl; }
  13.         void fun2() { std::cout << "B::fun2()" << std::endl; }
  14. };
  15.  
  16. void main(void)
  17. {
  18.         A a;
  19.         a.fun();                //      A::fun()
  20.  
  21.         B b;
  22.         b.fun();                //      B::fun()
  23.         b.fun2();              //     B::fun2()
  24.  
  25.         A* rA = &b;
  26.         rA->fun();            //    A::fun()
  27.         std::cout << &b << " " << rA << std::endl;
  28.        
  29.         B* rB = static_cast<B*> (&a);
  30.         rB->fun();            //    B::fun()
  31.         rB->fun2();          //   B::fun2()
  32.         std::cout << &a << " " << rB << std::endl;
  33.  
  34.         system("pause");
  35. }

多态:

  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. public:
  6.         virtual void fun(void) { std::cout << "A::fun()" << std::endl; }
  7. };
  8.  
  9. class B: public A
  10. {
  11. public:
  12.         void fun(void) { std::cout << "B::fun()" << std::endl; }
  13.         void fun2(void) { std::cout << "B::fun2()" << std::endl; }
  14. };
  15.  
  16. void main(void)
  17. {
  18.         A a;
  19.         a.fun();                //      A::fun()
  20.  
  21.         B b;
  22.         b.fun();                //      B::fun()
  23.         b.fun2();              //     B::fun2()
  24.  
  25.         A* rA = &b;
  26.         rA->fun();            //    B::fun()
  27.  
  28.         B* rB = static_cast<B*> (&a);
  29.         rB->fun();            //    A::fun()
  30.         rB->fun2();          //   B::fun2()
  31.  
  32.         system("pause");
  33. }

C++对象布局及多态实现的探索

系列文章:http://c.chinaitlab.com/special/dxdtbj/Index.html

(一)前言:http://c.chinaitlab.com/cc/basic/200906/787895.html
普通类对象:http://c.chinaitlab.com/cc/basic/200906/787895_2.html
普通继承类对象:http://c.chinaitlab.com/cc/basic/200906/787895_3.html

(二)虚函数的类的对象:http://c.chinaitlab.com/cc/basic/200906/787896.html
(三)虚函数的类的对象(2):http://c.chinaitlab.com/cc/basic/200906/787897.html

(四)类型动态转换和类型强制转换:http://c.chinaitlab.com/cc/basic/200906/787898.html

(五)普通成员函数的调用:http://c.chinaitlab.com/cc/basic/200906/787899.html

(六)虚函数调用:http://c.chinaitlab.com/cc/basic/200906/787900.html

(七)构造函数中的虚函数调用:http://c.chinaitlab.com/cc/basic/200906/787901.html

(八)普通的虚继承:http://c.chinaitlab.com/cc/basic/200906/787902.html

(九)菱形结构的虚继承:http://c.chinaitlab.com/cc/basic/200906/787903.html
(十)菱形结构的虚继承(2):http://c.chinaitlab.com/cc/basic/200906/787904.html
(十一)菱形结构的虚继承(3):http://c.chinaitlab.com/cc/basic/200906/787905.html

(十二)后记:http://c.chinaitlab.com/cc/basic/200906/787906.html

(十三)代码中定义的类的简单说明:http://c.chinaitlab.com/cc/basic/200906/787907.html

VS2005下各基本类型的size

系统信息
 操作系统 Windows XP Professional SP2
 编译环境 Microsoft Visual Studio 2005
 CPU Pentium(R) Dual-Core CPU E5200 @ 2.50 GHz

基本类型

 数据类型 大小 
 int  4
 char  1
 long  4
 double  8
 float  4
 short  2
 bool  1

【20090820】C++培训日记-进阶1

今天学习的内容:this指针、继承(不使用虚函数)、多态、单继承与虚函数表