在.h文件中一个类的定义,该类拥有多个方法和属性:
class Test1
{
public:
Test1(int n);
void set(int, int, int);
int isLeapYear();
void print();
private:
int num;
int month;
int day;
int year;
double length;
};
就是我在main函数中获取到了类Test1实例t1了,如何才能知道这个实例 中有哪些方法和属性?这些方法和属性有doc吗?怎么查看?
**是不是任何的c代码编译成lib和dll后都会有对应的.h头文件,这个头文件中就包含了类的方法和属性信息?
会不会存在没有头文件,但是我们可以正常使用lib和dll所提供的类?对于这样的情况,那么如何知道该类要如何调用,并能否查看方法的doc?
**
因为是从Python过来学C++的,以前学过c,现在回来学发现C++相对python是有点不方便,或者说我不会用,因为在python中查看类的属性方法和doc非常方便,一个dir函数就出来了:
#导入pandas这个包
import pandas as pd
#pd.DataFrame是一个类,使用dir查看这个类中有的方法和属性
dir(pd.DataFrame)
Out[3]:
['T',
'_AXIS_ALIASES',
'_AXIS_IALIASES',
'_AXIS_LEN',
'_AXIS_NAMES',
'_AXIS_NUMBERS',
'_AXIS_ORDERS',
'_AXIS_REVERSED',
'_AXIS_SLICEMAP',
'__abs__',
'__add__',
'__and__',
'__array__',
'__array_wrap__',
'__bool__',
'__bytes__',
...]
#在ipython中查看doc,就是加个问号就知道怎么用了
pd.DataFrame?
Init signature: pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
Docstring:
Two-dimensional size-mutable, potentially heterogeneous tabular data
structure with labeled axes (rows and columns). Arithmetic operations
align on both row and column labels. Can be thought of as a dict-like
container for Series objects. The primary pandas data structure
Parameters
----------
data : numpy ndarray (structured or homogeneous), dict, or DataFrame
Dict can contain Series, arrays, constants, or list-like objects
index : Index or array-like
Index to use for resulting frame. Will default to np.arange(n) if
no indexing information part of input data and no index provided
columns : Index or array-like
Column labels to use for resulting frame. Will default to
np.arange(n) if no column labels are provided
dtype : dtype, default None
Data type to force. Only a single dtype is allowed. If None, infer
copy : boolean, default False
Copy data from inputs. Only affects DataFrame / 2d ndarray input
Examples
--------
Constructing DataFrame from a dictionary.
>>> d = {'col1': [1, 2], 'col2': [3, 4]}
>>> df = pd.DataFrame(data=d)
>>> df
col1 col2
0 1 3
1 2 4
Notice that the inferred dtype is int64.
>>> df.dtypes
col1 int64
col2 int64
dtype: object
To enforce a single dtype:
>>> df = pd.DataFrame(data=d, dtype=np.int8)
>>> df.dtypes
col1 int8
---Return to continue, q to quit---
#使用type查看类型
In [5]: type(pd.DataFrame)
Out[5]: type
In [6]: type(pd)
Out[6]: module
那么c++中有没有类似的方法,或者说要是有这些需求要如何处理?
还是我理解错了,只要能用都有.h文件的?
谢谢
先对比一下 python 与 c++ 代码的运行方式。
python
python 解释器直接执行 python 源码,每一个被创建的 python 对象都包含有该类的属性名、方法名等信息,所以 dir 方法可以查到。
c++
c++ 编译器把 c++ 源码编译成机器码,它不保留类的属性名、方法名等信息,除非你要求它附带调试符号。
不过 dll 比较特殊,它可以附带一些导出(export)方法列表,里面包含有方法名称。
这可以通过 PE 工具看到,如 Dependency Walker, http://www.dependencywalker.com/
关于调试符号请查阅编译器的相关文档,如
需要深入研究 c++ 类如何转换成机器码的同学,可网上搜索 c++ virtual function 。
再来说一下 .h 头文件与 lib/dll 库文件的关系。
一般而言,头文件是库开发者给库调用者使用的一种声明文件,调用者的 c++ 编译器根据头文件生成必要的机器码以访问类库的属性或方法。
如果头文件丢失了,使用逆向工程(reverse engineering)可以造一个头文件出来。