0

Could you help me to understand the code below? I have an interface and need to write a code. But it first time when I work with OOP. I would be grateful if you answer some questions about this code.

template <class T, class Compare = std::less<T>>
class List_of_objects {
public:
  using IndexChange =
    std::function<void(const T& element, size_t new_element_index)>;

  explicit Heap(
    Compare compare = Compare(),
    IndexChange index_change = IndexChange());

// Other methods not important in my question

private:
IndexChange index_change_;
Compare compare_;

}

// Realization of methods
template<class T, class Compare>
List_of_objects<T, Compare>::List_of_objects(Compare compare, IndexChange index_change)
: compare_(compare), index_change_(index_change) {}

I have learned what std::function<> do from this site but I don't understand why we are writing using before 'IndexChange'

Also I have no idea what does compare_(compare) means, and what mean single colon in realization of List_of_objects()

I think that it is not difficult questions but I meet a deadline, I can't find answers in Stroustrup and in i-net:(

Thank you for any help!

alain
  • 11,939
  • 2
  • 31
  • 51
Acapello
  • 127
  • 9
  • 5
    Don't ask multiple questions as one question. Ask a question for each question. And anyway, all those are in your book. You just didn't look hard enough. – Bartek Banachewicz Nov 04 '15 at 15:36
  • Too broad? It's just two simple questions about the same piece of code. – alain Nov 04 '15 at 15:42
  • @alain: Yes, but do you think a question about `using` *and* member initializer lists, posted under the title "several questions about classes and OOP in C++" (which the question is not about...) will help future visitors? Wouldn't a question "what is 'using'?" and one of "what is this colon?" be better? (Not only because we could have closed each of them satisfyingly with a single duplicate link each?) – DevSolar Nov 04 '15 at 15:46
  • @DevSolar Hmm, ok, fair points. I didn't think about closing as duplicates, that makes sense. Maybe the OP could edit the title, such that it is more specific. (Else I'll do it in a moment). – alain Nov 04 '15 at 15:52

2 Answers2

1
using IndexChange = std::function<void(const T& element, size_t new_element_index)>;

permits you not to write the definition another time. This is a shortcut.

Then

IndexChange index_change_;

is the same as defining

std::function<void(const T& element, size_t new_element_index)> index_change_;

See : What is the logic behind the "using" keyword in C++?


Concerning compare_, it is the attribute where its type is defined by the template argument Compare in your definition, and by default it is std::less. So calling compare_() will call an instance of std::less or another class if you instantiate List_of_objects with another template argument. As for example : List_of_objects<int, MyComparator> then the class MyComparator will be used instead of std::less.

See : Default template arguments for function templates


The part with the semicolons will initialize the members of your class.

See : In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?

Community
  • 1
  • 1
dkg
  • 1,775
  • 14
  • 34
0
using IndexChange =
  std::function<void(const T& element, size_t new_element_index)>;

You can understand this as an abbreviation for a type. Instead of having to write std::function<..... you can simply use IndexChange, as it is done here:

  explicit Heap(
    Compare compare = Compare(),
    IndexChange index_change = IndexChange());

This is a constructor for List_of_objects:

List_of_objects<T, Compare>::List_of_objects(Compare compare, IndexChange index_change)
: compare_(compare), index_change_(index_change) {}

the part after the single colon initializes members with the parameters passed to the constructor.

alain
  • 11,939
  • 2
  • 31
  • 51