破译c++代码:结构中的函数与结构同名
Deciphering C++ code: function in struct with same name as the struct
我正在尝试学习编程。我从ai-junkie.com上得到这个代码。我不明白每个结构体的最后几行
- SNeuron (int NumInputs)
- SNeuronLayer(int NumNeurons, int NumInputsPerNeuron).
这样做的目的是什么?有人愿意教基础c++吗?
struct SNeuron {
//the number of inputs into the neuron
int m_NumInputs;
//the weights for each input
vector<double> m_vecWeight;
//ctor
SNeuron(int NumInputs);
};
struct SNeuronLayer {
//the number of neurons in this layer
int m_NumNeurons;
//the layer of neurons
vector<SNeuron> m_vecNeurons;
SNeuronLayer(int NumNeurons, int NumInputsPerNeuron);
};
这是一个构造函数。有人给它加了一个毫无意义的评论ctor
。它允许您使用如下代码实例化SNeuron
的实例
SNeuron sn(5);
这有助于程序的稳定性。在C语言中,您必须在实例化结构的实例后自己填充结构字段。这会使结构处于不明确的状态。在c++中,实例可以一步完成。
请记住,在c++中,struct
与class
完全相同:除了struct
中所有成员函数和成员数据默认为public
(而在class
中它们默认为private
)。
这是struct SNeuron
接受int
的C-tor。
在c++中,struct
就是包含所有成员public
的class
。像任何class
一样,它可以有C -tor, d- tor和其他c++元素。
相关文章:
- 使用不带参数的函数访问结构元素
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 链表,反向函数,数据结构
- 结构向量中自定义结构函数的内存使用
- C++ 中的嵌套结构函数
- 在结构函数之间传递文件路径 C++ 编辑:修复LNK2019错误
- 如何将值从外部传递到结构函数
- 如果存在具有不同参数的继承成员,为什么对 C++ 结构函数的调用不明确?
- C++结构函数的多个定义
- C++ 另一个标头中的结构函数给出"missing type specifier"
- 如何在C中找到结构/函数的父头文件
- C++二进制文件到结构函数
- C++ 将结构函数作为继承结构中函数的参数传递
- 如何在 main 之外的类中使用结构函数
- 在类中使用结构函数,在基类 c++ 中使用变量
- C++ 结构函数体调用另一个结构函数,同一个模块
- 是存储在对象中的类/结构函数
- Eclipse CDT 在从 std::map 访问时无法解析结构函数
- C++模板自身名称作为模板模板参数在同一模板结构函数内传递
- 如何使用真正的字符串类型来表示类结构函数中的字符串