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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:22:46+00:00 2026-05-27T20:22:46+00:00

The Google Style Guide for C++ says, in the section about smart pointers: We

  • 0

The Google Style Guide for C++ says, in the section about smart pointers:

  • We prefer designs in which objects have single, fixed owners.

I don’t fully understand the sentence.

  • What is the object owner?
  • Is it just the pointer?
  • 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-27T20:22:46+00:00Added an answer on May 27, 2026 at 8:22 pm

    The ‘owner’ of an object is not an actual part of the C++ language, but rather a conceptual tool – the idea being the ‘owner’ is in charge of deciding when the object can be destroyed. If an object has a single owner, it’s easy to figure out when the object needs to be destroyed – just look at the owner. When an object is referenced by multiple other objects, however, things are less clear – none of the referring objects can, on its own, delete the referee object, as this would cause problems for the other referers. So there there is no “single, fixed owner”.

    To give some examples, consider a ‘picture’ object.

    class Picture {
      char *rawData;
    public:
      Picture(char *rawData_) : rawData(rawData_) { }
      Picture(const Picture &p) : rawData(p.rawData) { }
    };
    

    Picture does not own rawData; as we can see from the copy constructor, it’s all too easy to have multiple Pictures referencing the same data. A better version might look like:

    class Picture {
      std::vector<char> rawData;
    public:
      Picture(const std::vector<char> &rawData_) : rawData(rawData_) { }
    };
    

    This is similar, but now vector is hiding the raw pointer for us; it’s impossible to have two Pictures reference the same pointer. And we take care of destroying the rawData array when the Picture is destroyed. In this case, the Picture owns the raw data.

    Now, you don’t have to use STL containers to have an owned object. You could do it manually:

    class Picture {
      size_t rawDataSize;
      char *rawData;
    
    public:
      Picture(size_t rds, char *rd) {
        rawDataSize = rds;
        rawData = new char[rds];
        memcpy(rawData, rd, rds);
      }
    
      ~Picture() { delete [] rawData; }
    
      Picture(const Picture &pic) {
        rawDataSize = pic.rawDataSize;
        rawData = new char[rawDataSize];
        memcpy(rawData, pic.rawData, rawDataSize);
      }
    
      Picture& operator=(const Picture &pic) {
        delete [] rawData;
        rawDataSize = pic.rawDataSize;
        rawData = new char[rawDataSize];
        memcpy(rawData, pic.rawData, rawDataSize);
        return *this;
      }
    };
    

    This is also an owning relationship; the rawData array ‘belongs’ to the picture object, permanently. All accesses go through the picture object, and it’s destroyed along with the picture object.

    In general, with owning relationships, it’s recommended to use wrapper classes to automate destruction, and prevent accidental copying. In my third example, had I forgotten the copy constructor or operator=, for example, terrible things would happen. Using wrapper classes like std::unique_ptr, boost::scoped_ptr/boost::scoped_array, or std::vector (for owned arrays) help prevent you from making mistakes.

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

Sidebar

Related Questions

In the Google JavaScript style guide, it says not to use wrapper objects for
In the Google C++ Style Guide , the section on Operator Overloading recommends against
In the Google C++ Style Guide, there's a section on Operator Overloading that has
I was looking at the Google C++ Style Guide and they have decided not
I have a Google style instant search script written in jQuery which pulls results
I have a Google Instant style search script written in jQuery which queries a
Generally I follow the Google style guide, which I feel aligns nicely with the
Why does the Google Python Style Guide prefer list comprehensions and for loops instead
I have an app that would benefit from google-style autocompletion while filling in an
Are there any good places to get examples for the C++ Google Style Guide?

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.