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 6847359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:44:07+00:00 2026-05-27T00:44:07+00:00

As I make the transition from C# to C++ I get a lot of

  • 0

As I make the transition from C# to C++ I get a lot of recommendations to use value semantics where possible. It’s pretty much guaranteed that if I post a question with a pointer anywhere someone will come along and suggest that it should be a value instead. I’m starting to see the light and I have found a lot of places in my code where I could replace dynamic allocation and pointers with stack allocated variables (and usually references). So I think I have a grasp on using stack allocated objects and passing them to other functions as references when the object lifetime is longer in the caller than the callee.

However I have a question about passing objects by value when the callee will take ownership. Take the following example:

class Zoo
{
  void AddAnimal(Animal animal);
  std::list<Animal> animals_;
}

Typically from a flexibility and unit testing perspective I’d want Animal to be an interface (abstract class in C++) so I can easily send arbitrary animals and mock it out with a mock implementation.

In a pointer implementation client code would be calling this like:

Animal animal = new Lion("Bob");
myZoo.AddAnimal(animal);

Here the client code doesn’t really need the animal object. It’s just constructing it temporarily to pass to the method. So in this case there aren’t shared semantics. So it seems like a good case for value semantics. However, my understanding is that you can’t use Animal as a parameter passed by value because it’s an abstract class.

Most of my member functions that don’t take primitive types take abstract class parameters. So what is the C++ method to handle this problem? (That is how do you program to interfaces in C++ with value semantics?)

  • 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-27T00:44:08+00:00Added an answer on May 27, 2026 at 12:44 am

    The typical solution for your scenario would involve a resource-managing handler object which you do pass by value. Popular candidates are shared_ptr and unique_ptr:

    #include <list>
    #include <memory>
    #include "all_zoo_animals.h"  // yours
    
    typedef std::shared_ptr<Animal> AnimalPtr;  // see note
    typedef std::list<AnimalPtr> AnimalCollection;
    
    AnimalCollection zoo;
    
    void addAnimal(AnimalPtr a)
    {
      zoo.push_back(a);
    }
    
    int main()
    {
      AnimalPtr a = AnimalPtr(new Penguin);
      a.feed(fish);
      addAnimal(a);  // from local variable, see note
    
      addAnimal(AnimalPtr(new Puffin)); // from temporary
    }
    

    If it is feasible, you could also define AnimalPtr as std::unique_ptr<Animal>, but then you have to say addAnimal(std::move(a));. This is more restrictive (as only one object handles the animal at any given time), but also lighter-weight.

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

Sidebar

Related Questions

I’m trying to use the proprietary iPhone Safari properties -webkit-transition and -webkit-transform to make
I'm trying to make a transition from vb.net to C++ and I'm stuck on
I've tried a few variations of using webkit-transition that I've found from googling but
I've started to make the transition to html5 and have been struggled to get
Can we make animations on the transition of MessageBox and change its default style
im planning to make a Simple Screen transition Dashboard. I am going to make
Make it on Linux. The reason to use more than one version of Vim,
i make one class file for jar. and this class have use wurfl. and
I make an app that have to printing image. I need to print image
To make my Ant Generated swf as small as possible, i used the Runtime

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.