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

The Archive Base Latest Questions

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

I have a Parent class with a number of children. Each function in the

  • 0

I have a Parent class with a number of children. Each function in the Parent class is pure i.e. there are no parent implementations of the functions but the children have their own implementations. No need to post code there – standard stuff.

I don’t want people creating direct instantiations of the parent class from anywhere. I have safe-guarded against this by having the virtual functions being pure so that’s fine.

My problem: Depending on a user’s input (a string), I want to instantiate one of the children. I only know which one at runtime. My (incorrect?) idea was the following which compiles fine and works fine until I put the code into a function and return the parent.

So this works:

Parent* parent;
if(user_input == "A") {
    Child1 child1;
    parent = &child1;
}
else if(user_input == "B") {
    Child2 child2;
    parent = &child2;
}

but this does not work:

Parent* foo(string user_input) {
    Parent* parent;
    if(user_input == "A") {
        Child1 child1;
        parent = &child1;
    }
    else if(user_input == "B") {
        Child2 child2;
        parent = &child2;
    }
   return parent;
}

When I say it doesn’t work I mean, it compiles but then I get a segmentation error when I do this:

Parent* parent = foo(user_input);
parent->some_child_function(); // segmentation error here

I’m sure it’s a stupid/simple question about me not fully understanding pointers??? Well after reading all about them in books/articles, I still don’t know what I’m doing wrong…. It’s probably a one-line fix?

Thank you :).

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

    You run into undefined behavior:

    Parent* parent;
    if(user_input == "A") {
        Child1 child1; // stack-allocated object
        parent = &child1;
    } //child1 is destroyed at this point, the pointer is dangling now
    

    you have to use new (preferably with a smart pointer):

    Parent* parent = 0;
    if(user_input == "A") {
       parent = new Child1();
    } else if(user_input == "B") {
       parent = new Child2();
    }
    

    and it’s also a good idea to set the pointer to null at the beginning so that you don’t have a pointer with a garbage value in case you have an error in your code and the pointer is left uninitialized.

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

Sidebar

Related Questions

Say I have a parent interface/class like so interface Parent<T> {} And a number
New to FluentNHibernate =D I have a parent/children classes as follows: public class Parent
I have a parent and child class that both need to implement IDisposable .
I'd like to have a parent class (Group) where any number of User may
I have common parent\children scenario: public class Order : AdvancedBaseOrder { ICollection<ProducerRelation> producers =
I have a parent class which contains a child object. I am using set
I have a parent class and child class (inherited from parent). In the child
I have one parent class, which is abstract class for now, for four different
I have a parent class Parent and a child class Child , defined thus:
In Django, when you have a parent class and multiple child classes that inherit

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.