A similar, maybe the same, question has been asked here: force const storing of returned by value value
However, much of the discussion was focused on "Why would you do that?"
Let's say I have struct A and B that both contain a shared_pointer. Now I have class C. Class C contains an instance of A. C has a const method getB() that return an object of type B, created on the moment and that contains a copy of the shared_pointer of A. However, I would like that whoever called getB() could not modify the content of B, especially the area of memory pointed by the shared_pointer.
In my user case, I am not in the position to modify class A and class B (which happen to be IplImage and cv::Mat of Opencv)
Is the only way to obtain my target to return a newly created B const *
? (In this respect, this is a very useful answer)