Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1305

Answer by HolyBlackCat for Creating a "view" of a sibling class avoiding unnecessary copy

$
0
0

A (somewhat cumbersome) solution is to give as_curvilinear_shape the following signature:

const CurvilinearShape &as_curvilinear_shape(CurvilinearShape &storage) const;

If it's already the right shape, return *this and leave storage unchanged. If a conversion is needed, assign the conversion result to storage and return a reference to storage.

Or you might want to create a helper class and return it:

struct CurvilinearShapeOrRef{    CurvilinearShape storage; // or `std::optional`/`boost::optional`.    const CurvilinearShape *ptr_override = nullptr; // C++98 can't do this, make a constructor.    const CurvilinearShape &get() const {return ptr_override ? *ptr_override : storage;}};

Viewing all articles
Browse latest Browse all 1305

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>