【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时递归遍历容器中的所有组件 如果是可输入组件则清除内容,可选择组件则清除选择。
-
import java.awt.Component;
-
import java.awt.Container;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
-
import javax.swing.JComboBox;
-
import javax.swing.JList;
-
import javax.swing.JTextArea;
-
import javax.swing.JTextField;
-
-
/**
-
* <p>Title: 重置按钮监听器</p>
-
*
-
* <p>Description: 单击重置按钮 清空窗体中数据</p>
-
*
-
* @author YOYO
-
*
-
* @create 2009-8-8
-
*
-
* @修改历史
-
* <li>版本号 修改日期 修改人 修改说明
-
* <li>
-
* <li>
-
*/
-
-
private Container container;
-
-
this.container = container;
-
}
-
-
/**
-
* 清空容器内容
-
* @param parent
-
*/
-
}
-
}
-
}
-
}
-
}
-
}
-
}
-
}
-
-
clear(container);
-
}
-
-
}
C++中的this指针
this总是指向当前对象,每次执行成员函数时,形如本例中的SetColor(int _color),总是会被编译成SetColor(CTestThis* this, int _color),编译器会自动带入对象地址作为this指针所指向的地址,本例中的全局函数也许可以加深对this指针的理解。
-
#include <iostream>
-
-
class CTestThis
-
{
-
public:
-
int GetColor() { return this->color; }
-
void SetColor(int _color) { this->color = _color; }
-
void Display() { std::cout << this->color << std::endl; }
-
-
private:
-
int color;
-
};
-
-
void SetColor(int _color, CTestThis* _)
-
{
-
_->SetColor(_color);
-
}
-
-
void Display(CTestThis* _)
-
{
-
std::cout << _->GetColor() << std::endl;
-
}
-
-
void main(void)
-
{
-
CTestThis test;
-
-
test.SetColor(3);
-
test.Display();
-
-
SetColor(33, &test);
-
Display(&test);
-
-
system("pause");
-
}
VS2005 反汇编
右键工具栏->自定义->命令->调试,将反汇编的按钮拖到工具栏上,单步调试项目,单击该按钮即可查看汇编代码 = =
C++的继承与多态demo
继承(无虚函数):
-
#include <iostream>
-
-
class A
-
{
-
public:
-
void fun() { std::cout << "A::fun()" << std::endl; }
-
};
-
-
class B: public A
-
{
-
public:
-
void fun() { std::cout << "B::fun()" << std::endl; }
-
void fun2() { std::cout << "B::fun2()" << std::endl; }
-
};
-
-
void main(void)
-
{
-
A a;
-
a.fun(); // A::fun()
-
-
B b;
-
b.fun(); // B::fun()
-
b.fun2(); // B::fun2()
-
-
A* rA = &b;
-
rA->fun(); // A::fun()
-
std::cout << &b << " " << rA << std::endl;
-
-
B* rB = static_cast<B*> (&a);
-
rB->fun(); // B::fun()
-
rB->fun2(); // B::fun2()
-
std::cout << &a << " " << rB << std::endl;
-
-
system("pause");
-
}
多态:
-
#include <iostream>
-
-
class A
-
{
-
public:
-
virtual void fun(void) { std::cout << "A::fun()" << std::endl; }
-
};
-
-
class B: public A
-
{
-
public:
-
void fun(void) { std::cout << "B::fun()" << std::endl; }
-
void fun2(void) { std::cout << "B::fun2()" << std::endl; }
-
};
-
-
void main(void)
-
{
-
A a;
-
a.fun(); // A::fun()
-
-
B b;
-
b.fun(); // B::fun()
-
b.fun2(); // B::fun2()
-
-
A* rA = &b;
-
rA->fun(); // B::fun()
-
-
B* rB = static_cast<B*> (&a);
-
rB->fun(); // A::fun()
-
rB->fun2(); // B::fun2()
-
-
system("pause");
-
}
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指针、继承(不使用虚函数)、多态、单继承与虚函数表