Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6072715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:11:26+00:00 2026-05-23T10:11:26+00:00

Suppose that Foo is a rather large data structure. How should I write a

  • 0

Suppose that Foo is a rather large data structure. How should I write a const virtual function that returns an instance of Foo, if I don’t know whether the inherited classes will store the instance of Foo internally; thus, allowing a return by reference. If I can’t store it internally, my understanding is I can’t return a const reference to it because it will be a temporary. Is this correct? The two options are:

virtual Foo foo() const { ... }
virtual Foo const & foo() const { ... }

Here’s a related question but from a different angle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:11:27+00:00Added an answer on May 23, 2026 at 10:11 am

    You’re interested in the difference between a value return and a const reference return solely as a matter of optimization, but it isn’t. There’s a fundamentally different meaning between returning a different value each time, vs. returning a reference each time, quite possibly to the same object, which quite possibly could be modified:

    const Foo &a = myobj.foo();
    myobj.modify_the_foo();
    const Foo &b = myobj.foo();
    a == b; // do you want this to be true or false?
    

    The caller needs to know which it is, both because the programmer needs to know the meaning and because the compiler needs to know the calling convention, so you can’t mix them in different overrides of the same virtual function. If some derived classes want to do one, and some want to do the other, then that’s tough luck, they can’t, any more than one can return an int and another a float.

    You could perhaps return a shared_ptr. That way, the derived classes that “want” to return a reference can create a shared_ptr with a deleter that does nothing (but beware – the shared_ptr will dangle if the original object is destroyed, and that’s not what you normally expect from a returned shared_ptr. So if it makes sense for the Foo to outlive the object it came from then it would be better for the class to dynamically allocate it, hold it via a shared_ptr, and return a copy of that, rather than a do-nothing deleter). The derived classes that “want” to return a value can allocate a new one each time. Since Foo is “rather large”, hopefully the cost of the shared_ptr and the dynamic allocation isn’t too painful compared with what you’d do anyway to create a new value to return.

    Another possibility is to turn Foo into a small pImpl-style class that references a rather large data structure. If everything involved is immutable, then the “want to return a reference” case can share the large data structure between multiple Foo instances. Even if it isn’t, you can think about copy-on-write.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following class Foo, that supports a function of any arity
Suppose that the function void foo(int n, double x[]) sorts the n-vector x ,
Suppose that I have the model Foo in GAE and this query: query =
Lets suppose that I have some pages some.web/articles/details/5 some.web/users/info/bob some.web/foo/bar/7 that can call a
Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)>
Let's suppose I have a bash script ( foo.sh ) that in a very
Suppose I have: var myfunc = function() { // do stuff } myfunc.foo =
suppose i have a class Foo and an instance of this class myFoo: Foo
Suppose that a group wants to encrypt some information, then share the encryption key
Suppose that two tables exist: users and groups . How does one provide simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.