简体   繁体   English

C ++类初始化

[英]C++ class initialization

NOVICE ALERT. NOVICE ALERT。 I have the following classes 我有以下课程

View 视图

class View {
 public:
  explicit View(const TupleSchema& schema)
      : schema_(schema),
        columns_(new Column[schema.attribute_count()]),
        row_count_(0) {
  }
  View(const View& other)
      : schema_(other.schema()),
        columns_(new Column[other.schema().attribute_count()]),
        row_count_(0) {

  }
  explicit View(const View& other, rowcount_t offset, rowcount_t row_count)
      : schema_(other.schema()),
        columns_(new Column[other.schema().attribute_count()]),
        row_count_(0) {

  }
     View(const Column& column, rowcount_t row_count)
      : schema_(TupleSchema::Singleton(column.attribute().name(),
                                       column.attribute().type(),
                                       column.attribute().nullability())),
        columns_(new Column[1]),
        row_count_(row_count) {

  }

 private:
  const TupleSchema schema_;
  scoped_array<Column> columns_;
  rowcount_t row_count_;
};

Block

class Block {
 public:
  Block(const TupleSchema& schema, BufferAllocator* allocator)
      : allocator_(allocator),
        columns_(new OwnedColumn[schema.attribute_count()]),
        view_(schema) {

    }
  }
}
 private:
  BufferAllocator* const allocator_;
  scoped_array<OwnedColumn> columns_;
  View view_;  // Full view on the entire block.
  DISALLOW_COPY_AND_ASSIGN(Block);
};

View Copier 查看复印机

class ViewCopier : public BaseViewCopier {
 public:
  ViewCopier(const TupleSchema& schema, bool deep_copy);
  ViewCopier(const BoundSingleSourceProjector* projector, bool deep_copy);
};

When i use the above as members in another class and i write a constructor for it like below 当我使用上面作为另一个类的成员,我写下一个构造函数,如下所示

class SegmentedTable : public BasicOperation {
public:
    SegmentedTable::SegmentedTable(const std::vector<TupleSchema> vp_schemas, BufferAllocator* buffer_allocator)
      : BasicOperation(),
        view_copier_(NULL, NULL) { }
private:
    scoped_ptr<Block> block_;
    View view_;
    ViewCopier view_copier_;
}

i get an error message that no method View::View() is defined. 我收到一条错误消息,没有定义View :: View()方法。 I understand it is because there is no View() constructor of class which is needed because it gets automatically initialized in the SegmentedTable Constructor's initializer list. 我理解这是因为没有类的View()构造函数,因为它在SegmentedTable构造函数的初始化列表中自动初始化。 However i have 2 questions 但是我有2个问题

1) why isn't the same needed for Block. 1)为什么Block不一样。

2) why can i initialize ViewCopier with ViewCopier(NULL, NULL) while i can't do that for View. 2)为什么我可以使用ViewCopier初始化ViewCopier(NULL,NULL)而我无法为View执行此操作。 Doing View(NULL) also tells me no method View::View(NULL) is defined. 执行视图(NULL)也告诉我没有定义View :: View(NULL)的方法。

I know i have not provided some other class definitions that are used in the example, but i am hoping the question can be answered without them. 我知道我没有提供示例中使用的其他类定义,但我希望没有它们可以回答这个问题。

It is because when you write: 这是因为你写的时候:

View view_;

It doesn't create a empty reference like in java, it actually tries to contruct View. 它不像java那样创建一个空引用,它实际上试图构造View。

So you should either use a pointer and instanciate it later, or constructing it passing the required parameters. 因此,您应该使用指针并稍后对其进行实例化,或者构建它并传递所需的参数。

Or add a constructor to View that doesn't take any parameters. 或者向View中添加一个不带任何参数的构造函数。

Same thing for ViewCopier ViewCopier

"However why isn't the same needed for Block." “但是为什么Block不一样。”

Because in the initializer list in Block 's constructor, you call the TupleSchema constructor for view_ : 因为在Block的构造函数的初始化列表中,您为view_调用TupleSchema构造view_

view_(schema) {

"Also why can i intialize ViewCopier with ViewCopier(NULL, NULL) " “还有为什么我intialize ViewCopierViewCopier(NULL, NULL)

Because NULL is implicitly convertible to bool . 因为NULL可以隐式转换为bool You are actually calling this constructor: 你实际上是在调用这个构造函数:

ViewCopier(const BoundSingleSourceProjector* projector, bool deep_copy);

To prevent this from happening, you can use C++11's nullptr instead of NULL. 为了防止这种情况发生,您可以使用C ++ 11的nullptr而不是NULL。 If you tried this: 如果你试过这个:

ViewCopier(nullptr,nullptr)

you would get a compiler error. 你会得到一个编译错误。 That's because nullptr won't implicitly convert to bool . 那是因为nullptr不会隐式转换为bool

  • Since you declared a pointer to a Block, at no point you tried to construct one (block_ will be initialized to nullptr), so the compiler won't complain about a missing constructor. 由于您声明了一个指向Block的指针,因此您没有尝试构造一个(block_将初始化为nullptr),因此编译器不会抱怨缺少构造函数。

  • ViewCopier(NULL, NULL) will call ViewCopier(const BoundSingleSourceProjector* projector, bool deep_copy); ViewCopier(NULL, NULL)将调用ViewCopier(const BoundSingleSourceProjector* projector, bool deep_copy); ; ; the second parameter is cast to a bool . 第二个参数被强制转换为bool View has no constructor that takes a pointer; View没有带指针的构造函数; only references. 只有参考。

You can't pass NULL to a constructor or a function that takes a reference; 您不能将NULL传递给构造函数或带引用的函数; you have to pass something. 必须传递一些东西。

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

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