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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:59:16+00:00 2026-06-13T04:59:16+00:00

What confused me is that I don’t want to create a object with a

  • 0

What confused me is that I don’t want to create a object with a pointer like ‘Product_ptr productA’, are there some other methods? Another questions is that all my products use DoSomething(), but I also want to add different attributes to different products, how to achieve this? Thanks for your suggestions!!

  • 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-06-13T04:59:17+00:00Added an answer on June 13, 2026 at 4:59 am

    Generally you cannot avoid pointers in C++ when dealing with dynamically created objects. You have to manage and pass ownership for such objects which is naturally done with pointers, mainly smart pointers, of course.

    Despite there are some ways to hide them, e.g. maintaining ownership of objects in some central point (factory) and pass them to consumers by reference. Such way has several drawbacks, e.g. consumer need explicitly release the object so the factory can destroy it and not waste resources. But if your objects are lightweight and their lifetime is the same as lifetime fo entire program or specific factory, this could be useful.

    Example:

    template <class Product>
    class Factory
    {
       // List elements are unaffected by insertion/deletion
       // Could be also container of smart pointers if objects need
       // to be created directly on heap
       std:list<Product> m_objects;
    
    public:
    
       Product& CreateProduct()
       {
          m_objects.push_back(Product());
          return m_objects.back();
       } 
    
    };
    
    // Usage
    Factory<MyProduct> factory;
    MyProduct& prod = factory.CreateProduct();
    ...
    

    This solution is possible, but has limitations, don’t use it without real need.
    Returning appropriate smart pointer from factory, e.g. std::shared_ptr is preferable because it gives you explicit semantics on object ownership and makes code more clear, maintanable and error-proof.

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

Sidebar

Related Questions

Possible Duplicate: Concatenate Two NSDate String values Just confused that is there any default
I have functions that return an object, but I'm confused that should I return
Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
I recently saw some Perl code that confused me. I took out all of
I am confused that in which file i need to create symfony functions. E,g
I'm working on a proprietary unix-like os (I don't know if that's relevant though)
Before I begin I want to make it clear that I don't want the
I am confused ( that is easy enough!). I have a small snippet of
I was confused that I am no idea for this problem. I am trying
I was confused that a thread is automatically stops after executing the return statement

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.