为什么这个c++函数是结构体

Why this c++ function is a struct

本文关键字:结构体 函数 c++ 为什么      更新时间:2023-10-16

我正在学习c++,并且对使用信任库的c++代码有点困惑。以下是我的问题:

  1. 为什么使用关键字struct
  2. 为什么函数is_word_start有冒号部分::public thrust::binary_function"

    // determines whether the right character begins a new word
    struct is_word_start
        : public thrust::binary_function<const char&, const char&, bool>
    {
        __host__ __device__
        bool operator()(const char& left, const char& right) const
        {
            return is_alpha(right) && !is_alpha(left);
        }
    };
    
https://github.com/thrust/thrust/blob/master/examples/word_count.cu

  1. struct的含义与class几乎相同,只是默认情况下struct的所有成员都是public,而在class中它们是private。(你可以像在类中那样在结构中指定访问修饰符。)
  2. : public部分表示公共继承。你应该好好研究一下这个和其他的继承类型。

该类重载一个所谓的调用操作符,因此该类的对象可以作为函数调用(在本例中带有两个实参)。这样的对象通常被称为函子,并在stl和其他任何地方广泛使用。