data-member-pointers

C++ Best way to have class member which is a pointer (or reference) to another class and can handle both const and non-const situations

不想你离开。 提交于 2021-02-11 12:42:48
问题 Consider the following sample code. It compiles and works as expected. However, if I add "const" to the beginning of the first line of main function, it will not compile, because B class takes a pointer to A , and with const being added, we will have a pointer to const A instead. Templatizing B is an option, but I wonder if there are cleaner/nicer solutions for this. I am open to various suggestions, including moving the B inside A if that would help (B is used by A only). #include <vector>