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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:05:57+00:00 2026-06-13T12:05:57+00:00

First off I am new to C++, especially using C++ in an OOP fashion.

  • 0

First off I am new to C++, especially using C++ in an OOP fashion. I have a class with multiple subclasses, and I was wondering if I could ambiguously declare a variable to accept an object without limiting the which objects can be stored in it. I am asking because one of the multiple children will end up being used at a time. So if I cannot ambiguously declare a variable I a way to determine which of the numerous variables are in use.

Something along the lines of

obj randomObj = new className;

instead of

className randomObj = new className

  • 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-13T12:05:58+00:00Added an answer on June 13, 2026 at 12:05 pm

    You say you’re new to C++, and the syntax you use to describe what you want suggests you’re more familiar with another languages like Java or C#. The syntax you show works fine in those languages:

    Foo myFoo = new DerivedFoo;
    

    This works because, behind the scenes in Java and C# myFoo actually behaves as a pointer to a Foo rather than as name for a fixed memory region capable of storing a Foo. In C++ the syntax Foo myFoo creates such a fixed memory region. Even if you try to put some derived type there by doing this:

    DerivedFoo myDerivedFoo;
    Foo myFoo = myDerivedFoo;
    

    myFoo can still only hold a Foo object. Everything that’s not a Foo is ‘sliced’ away during the initialization of myFoo, because it just doesn’t fit in that fixed memory region..

    So in C++ you have to explicitly do what Java and C# do behind the scenes by using C++’s pointer syntax:

        Foo *myFoo = new DerivedFoo;
    

    Now myFoo is a pointer to a Foo, and that pointer can refer to a any Foo object, including the Foo parts of a DerivedFoo, without any slicing or anything occurring. new DerivedFoo creates a memory region where a DerivedFoo can exist, and then myFoo is set to point at the Foo part of the created DerivedFoo.

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

Sidebar

Related Questions

First off, I'm new to Java, so please be gentle. I have a JFrame
First off, I am new to programming (especially with C#) and thanks for your
First off I am completely new to Javascript but I have some HTML/CSS experience.
First off, a lil about me, i'm very new to GUI programming, especially with
First off I am brand new to C and have been thrown into a
First off, I'm completely new to Drupal, so I could be making a ridiculously
First off I am pretty new to C, so I probably just have a
First off, I am a totally new developer with regards to Node.js. I have
First off, I'm new to Spring and I don't have my head quite wrapped
First off, this is my first project using SQLAlchemy, so I'm still fairly new.

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.