rvt / dxfrwtest

6 stars 8 forks source link

How to understand ': dxfW(dxfW) ' in ExampleWriter::ExampleWriter(dxfRW &dxfW) : dxfW(dxfW) { } #1

Closed kematy closed 5 years ago

kematy commented 5 years ago

I am a C++ beginner, I don't know what is the meaning of ': dxfW(dxfW) ' in ExampleWriter::ExampleWriter(dxfRW &dxfW) : dxfW(dxfW) { } How to understand it?

rvt commented 5 years ago
ExampleWriter::ExampleWriter(dxfRW &dxfW) : dxfW(dxfW) {
}

is the implementation of a constructor. You can recognise a constructor that the part before the :: is the same as the part after the :: (ExampleWriter::ExampleWriter)

dxfRW is a class type and &dxfW is a reference to that class type. (remember class names should start with capital letters, but the designer of dxfRW decided to make it lower case.

Then : dxfW(dxfW) means we assign the reference dxfW to the class variable dxfW. The part between : and { eg dxfW(dxfW) is called member initializer lists. see https://en.cppreference.com/w/cpp/language/initializer_list

So in the end the object ExampleWriter is constructed and the variable dxfW is assigned.

kematy commented 5 years ago

@rvt ,Thanks for your detailed explanation, I have understood that this is a way of writing initialization parameters.