简体   繁体   中英

Can a function inherit arguments?

In the source code for KinFu remake I found the following structure:

kfusion::OpenNISource::OpenNISource() : depth_focal_length_VGA (0.f), baseline (0.f),
shadow_value (0), no_sample_value (0), pixelSize (0.0), max_depth (0) {}

The parameters following the : are declared inside the class kfusion::OpenNISource. The function OpenNISource() is declared in this class as well. As I am new to C++, I figured the function inherits and declares the variables... (?)

So would the following code add the variables (foo, bar) and values (0, 1.0f) to the function?

class A 
{
  public:
    fn ();
    fn (int qux);

    int foo;
    float bar;
};


A::fn() : foo(0), bar(1.0f) {} 

And if so, would these variables be added to fn(int qux) as well??

Because this function has the same name as the class, it is the class's constructor , called automatically when the class is instantiated.

The variable list after the colon is the initializer list, used to assign the initial values to the class's member variables.

You can only do this with constructors, not just any function, so your code will not work. You would have to call the function "A" in order to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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