简体   繁体   中英

C++ class initialization

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. 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. However i have 2 questions

1) why isn't the same needed for Block.

2) why can i initialize ViewCopier with ViewCopier(NULL, NULL) while i can't do that for View. Doing View(NULL) also tells me no method View::View(NULL) is defined.

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.

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.

Same thing for ViewCopier

"However why isn't the same needed for Block."

Because in the initializer list in Block 's constructor, you call the TupleSchema constructor for view_ :

view_(schema) {

"Also why can i intialize ViewCopier with ViewCopier(NULL, NULL) "

Because NULL is implicitly convertible to 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. If you tried this:

ViewCopier(nullptr,nullptr)

you would get a compiler error. That's because nullptr won't implicitly convert to 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.

  • ViewCopier(NULL, NULL) will call ViewCopier(const BoundSingleSourceProjector* projector, bool deep_copy); ; the second parameter is cast to a bool . View has no constructor that takes a pointer; only references.

You can't pass NULL to a constructor or a function that takes a reference; you have to pass something.

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