简体   繁体   English

结构C ++表示卡住

[英]Structure C++ meaning stuck

This is really question on C++ language, what does SNeuron(int NumInputs); 这真的是关于C ++语言的问题, SNeuron(int NumInputs);是什么SNeuron(int NumInputs); mean inside the structure? 是指结构内部? I'm not c++ programer and this construction inside structure looks strange to me. 我不是c ++程序员,这种内部构造对我来说很奇怪。 Can anybody explain it what it could be for? 有人可以解释它的用途吗? I already tried google. 我已经尝试过谷歌。

 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);
    };

A struct in C++ is exactly the same as a class, except that all members of a struct are public by default. C ++中的结构与类完全相同,但默认情况下,结构的所有成员都是公共的。

So what you are seeing here is simply a constructor declaration for the struct. 因此,您在这里看到的只是该struct的构造函数声明。

The reason, I believe, is to make interoperability with C easier. 我相信,这样做的原因是为了简化与C的互操作性。

It's simply declaring a constructor for the struct SNeuron. 它只是在声明SNeuron的构造函数。 This is called a prototype method, and won't wok unless it's implemented later on. 这称为原型方法,除非稍后实现,否则不会生效。 It could be implemented inside the class by saying 它可以通过在课堂上说

SNeuron(int NumInputs) {
    // Constructor code
}

or outside like this: 或像这样的外部:

SNeuron::SNeuron(int NumInputs) {
    // Constructor code
}

The main use of this would be to initialize the fields m_NumInputs and m_vecWeight. 此方法的主要用途是初始化字段m_NumInputs和m_vecWeight。

When an instance of the structure is created, it need to be "constructed" (ie initialized), that is done by having constructor functions, which are automatically called by the compiler when an instance is created. 创建该结构的实例时,需要对其进行“构造”(即初始化),即通过具有构造函数来完成,构造函数会在创建实例时由编译器自动调用。

For example, in the following declaration and definition of a variable using the structure, the constructor will be called: 例如,在以下使用该结构的变量声明和定义中,将调用构造函数:

SNeuron myNeuron(5);  // Creates the instance and calls the constructor function

That is simply a constructor. 那只是一个构造函数。 A constructor is basically a mechanism by which you initialize all the data members of a class when an object of that class type is created. 构造函数基本上是一种机制,通过该机制,您可以在创建该类类型的对象时初始化该类的所有数据成员。

You can write constructors for both struct and class. 您可以为struct和class编写构造函数。 But that constructor which you declared in your code is not the default constructor because a default constructor is that constructor which takes no arguments. 但是,您在代码中声明的构造函数不是默认构造函数,因为默认构造函数是不带参数的那个构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM