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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:12:29+00:00 2026-05-19T14:12:29+00:00

A newbie question: I have a hierarchy of classes with some virtual functions and

  • 0

A newbie question: I have a hierarchy of classes with some virtual functions and I am trying to implement a factory method, but I am not sure what is the best way:

  1. Return a raw pointer from the factory method and wrap it into a smart pointer in the calling method
  2. Return a smart pointer from the factory
  3. Return a proper object from the factory (but is it going to copy the derived class correctly?) and assign it to a local object in the calling method
  4. Return a reference from the factory (but how to create the object in the factory method without a memory leak?)

I would be grateful for an example of a factory method and a minimal client, which is effective and doesn’t leak memory.

My background is C# and Java, so I am a bit lost with memory management in C++ atm.

  • 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-19T14:12:30+00:00Added an answer on May 19, 2026 at 2:12 pm

    Options 3 and 4 are out from the start because they simply don’t work: 3 slices the object, 4 creates memory leaks or invalid references.

    From the other two methods, I strongly prefer 2: return a smart pointer. In fact, strive to avoid raw pointers completely, if possible. Unfortunately, C++ makes this a lot to write (and this is not a trivial objection! Anybody who has ever written object-oriented code in C++ and has used smart pointers throughout shares my pain) – but the alternative is even more pain.

    Of course, there is alternative 5: use raw pointers and a garbage collector.

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

Sidebar

Related Questions

Newbie question here but for some reason I cant figure this out. I have
Probably this is a newbie question :) But I have some Flash games built
Newbie question. I have two c# classes - a code class (say, CodeClass) and
I have a newbie question. If I have some global variables that are shared
This may be a newbie question. I've used javascript for years but have only
newbie question I have a multiselect box that i populate with some ajax and
Newbie question, but I have this code: import java.util.*; import java.io.*; public class Welcome1
Newbie Question: I have a dictionary rendering with extra_Context from a method defined in
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I have a newbie question about integrating two iOS apps. I have created an

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.