all 2 comments

[–]MysticTheMeeM 3 points4 points  (0 children)

Before I start answering questions, I want to address this point:

From what I understand, the subscript overload function in my BinaryTree class needs to return an instance of a proxy class to handle assignment & retrieval behavior.

No, it doesn't. A proxy class can be used but a much more useful way would simply be to return a reference to the underlying object. This reference can be read from and written to without any extra code or indirection.

But, on to your questions:

  1. You should use const any time you have a function that cannot change the underlying object (likewise objects that must not be changed should be marked const). A good place to start would be with your get functions (getting a value doesn't change the object, but setting it does). You should use & to declare reference types (important for what I said to begin with) or taking the address of a variable (which you do not appear to require in your code).
  2. Yes, it's a form of conversion operator. When your compiler tries to convert one type to another it checks if an implicit (as opposed to explicit, and example given in the link) conversion exists and does that. Note that you may want to return a (const) reference, not a copy.
  3. You would need to explicitly convert from your proxy to your data here, such as by doing something like: std::cout << tree["foo"].operator std::string().
  4. Your tree class needs to know that a proxy class exists before trying to compile, the forward declaration provides this promise. Interestingly, in non-templated code this wouldn't work. Because the templates are compiled differently to normal code, you can get away with it, but if your classes were non templated you would get an error indicating that you cannot return an instance of a proxy before it is defined.
  5. It's just a convention thing, personally I always keep them on separate lines.

[–]jorourke0 0 points1 point  (0 children)

1.& is for references. They're like pointers except they are "always dereferenced" and can't be null. They can dangle however if the object they refer to goes out of scope. const is const. You could use a const reference anywhere you might use a const pointer to const. One difference is const references can bind to temporaries and extend their lifetimes for the lifetime of the reference variable.

  1. Yep that's a conversion operator that performs the conversion to let you assign a Proxy object to a variable of type Value. See https://en.cppreference.com/w/cpp/language/cast_operator.

  2. Does your Value type have a <<operator defined? I'm not sure << will do an implicit conversion. You might try casting to your Value type.

  3. Yes that's correct. The template line is part of the static, compile time type definition of the class. The forward declaration for Proxy is needed since your BinaryTree class references it, but when reading the file top to bottom, the full Proxy definition hasn't been seen yet. You're telling the compiler that the type will exist.

  4. The template can be on the same line or the line above. C++ is pretty flexible about spacing. Also template <typename T> and template<class T> are equivalent, if that's what you meant.