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

  • Home
  • SEARCH
  • 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 6150725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:39:27+00:00 2026-05-23T19:39:27+00:00

I came accross several questions where answers state that using T* is never the

  • 0

I came accross several questions where answers state that using T* is never the best idea.

While I already make much use of RIIC, there is one particular point in my code, where I use T*. Reading about several auto-pointers, I couldn’t find one where I’d say that I have a clear advantage from using it.

My scenario:

class MyClass
{

  ...
  // This map is huge and only used by MyClass and 
  // and several objects that are only used by MyClass as well.
  HashMap<string, Id> _hugeIdMap;
  ...
  void doSomething()
  {
    MyMapper mapper;
    // Here is what I pass. The reason I can't pass a const-ref is
    // that the mapper may possibly assign new IDs for keys not yet in the map. 
    mapper.setIdMap(&_hugeIdMap);
    mapper.map(...);
  }
}

MyMapper now has a HashMap<...>* member, which – according to highly voted answers in questions on unrelated problems – never is a good idea (Altough the mapper will go out of scope before the instance of MyClass does and hence I do not consider it too much of a problem. There’s no new in the mapper and no delete will be needed).

So what is the best alternative in this particular use-case?

  • 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-23T19:39:27+00:00Added an answer on May 23, 2026 at 7:39 pm

    Personally I think a raw pointer (or reference) is okay here. Smart pointers are concerned with managing the lifetime of the object pointed to, and in this case MyMapper isn’t managing the lifetime of that object, MyClass is. You also shouldn’t have a smart pointer pointing to an object that was not dynamically allocated (which the hash map isn’t in this case).

    Personally, I’d use something like the following:

    class MyMapper
    {
    public:
        MyMapper(HashMap<string, Id> &map)
            : _map(map)
        {
        }
    private:
        HashMap<string, Id> &_map
    };
    

    Note that this will prevent MyMapper from having an assignment operator, and it can only work if it’s acceptable to pass the HashMap in the constructor; if that is a problem, I’d make the member a pointer (though I’d still pass the argument as a reference, and do _map(&map) in the initializer list).

    If it’s possible for MyMapper or any other class using the hash map to outlive MyClass, then you’d have to start thinking about smart pointers. In that case, I would probably recommend std::shared_ptr, but you’d have to use it everywhere: _hugeIdMap would have to be a shared_ptr to a dynamically allocated value, not a regular non-pointer field.


    Update:

    Since you said that using a reference is not acceptable due to the project’s coding standards, I would suggest just sticking with a raw pointer for the reasons mentioned above.

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

Sidebar

Related Questions

I came accross a very strange problem with tr1::regex (VS2008) that I can't figure
Since I started learning Django framework I came across several applications that switched from
I came across a paper that implemented SVM using SMO. I planned to implement
I've came accross this concept several times and did not find a clear explanation.
I came across several interfaces while learning JDBC - Connection, Statement, ResultSet etc... Does
I came accross this html multiple file upload tutorial: http://robertnyman.com/2010/12/16/utilizing-the-html5-file-api-to-choose-upload-preview-and-see-progress-for-multiple-files/ I'm new to web
I came accross this code on the web: is_char(Ch) -> if Ch < 0
I came accross PetaPoco and decided to give it a try Did anyone come
I came accross the term Modular Extension as a requirement of an application of
I came accross this on the Mike Ash Care and feeding of singletons and

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.