Guess that Foo is really a big data structure. How must i write a const virtual function that returns a clear case of Foo, if I'm not sure if the inherited classes will keep demonstration of Foo internally thus, permitting coming back by reference. Basically can't store it internally, my understanding is I can not return a const mention of the it because it will likely be a brief. Is correct? The 2 choices are:

virtual Foo foo() const 

virtual Foo const &lifier foo() const 

Here is a related question but from the different position.

You are thinking about the main difference from a value return along with a const reference return exclusively ought to be optimisation, however it is not. There is a essentially different meaning between coming back another value every time, versus. coming back a reference every time, potentially towards the same object, which potentially might be modified:

const Foo &a = myobj.foo()

myobj.modify_the_foo()

const Foo &b = myobj.foo()

a == b // would you like this to be real or false?

The caller must know so it is, both since the programmer must be aware of meaning and since the compiler must be aware of calling convention, which means you can't mix them in various overrides of the identical virtual function. If some derived classes wish to accomplish one, plus some wish to accomplish another, then that's tough luck, they cannot, any several can return an int and the other a float.

You can possibly return a shared_ptr. This way, the derived classes that "want" to come back a reference can produce a shared_ptr having a deleter that does nothing (but beware - the shared_ptr will dangle when the original object is destroyed, and that is not that which you normally expect from the came back shared_ptr. Therefore if it seems sensible for that Foo to live the item it originated from then it might be better for that class to dynamically allocate it, hold it using a shared_ptr, and return a duplicate of this, as opposed to a do-nothing deleter). The derived classes that "want" to come back something can allocate a replacement every time. Since Foo is "bigInch, hopefully the price of the shared_ptr and also the dynamic allocation is not too painful in comparison using what you'd do anyway to produce a new value to come back.

Another possibility would be to turn Foo right into a small pImpl-style class that references an extremely large data structure. If everything involved is immutable, then your "wish to return a reference" situation can share the big data structure between multiple Foo instances. Even when it is not, you are able to consider copy-on-write.

I observe that you have not listed C++0x like a tag, but as reference for anybody together with your needs plus use of C++0x, possibly the easiest way would be to return a std::unique_ptr<>.

Here's one of the ways:

struct K

 

class I

  

class Impl : public I

  

  K kk

  

What causes it to be work is you use K kk within the same object as data member. A constructor for Impl class may be helpful too.

EDiT: altering formatting from the code