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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:41:19+00:00 2026-06-11T13:41:19+00:00

i am pretty sure this is a simple question for a long time c++

  • 0

i am pretty sure this is a simple question for a long time c++ user, this should be a pattern or the problem should be solved in any other way but given i am Python developer and a total novice with c++ i don’t know how it’s usually done.

Suppose that i have a class where i want to store a pointer to an object that can be of 1 of two different classes that respects an interface, for example:

class AllPlayers
{
  public:
  virtual void play();
};

class VlcPlayer: public AllPlayers
{
  public:
  virtual void play();
};

class Mplayer: public AllPlayers
{
  public:
  virtual void play();
};

class MyMediaPlayer
{
  public:
  MyMediaPLayer(int playerType);
  AllPlayers m_player;
};

MyMediaPlayer::MyMediaPlayer(int PlayerType)
{
  if (PlayerType == 0) {
    VlcPlayer tmp_player;
    m_player = static_cast<AllPlayers> (tmp_player);
  }
  else {
    Mplayer tmp_player;
    m_player = static_cast<AllPlayers> (tmp_player);
  }
}

MyMediaPlayer test(0);
test.play();

First, i know this would not work and that it seems pretty normal why but how could i get this effect? i would like to have a member of a class for what i am going to use ever the same methods, implemented using a interface and i would like to avoid trying to cast to every of the derived classes every time i am going to use one of his methods.

  • 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-11T13:41:19+00:00Added an answer on June 11, 2026 at 1:41 pm

    C++ is value-based, i.e., if you create an object of a given type you really have an object of this type. This doesn’t play nicely with dynamic polymorphism. To get dynamic polymorphism you use a pointer or a reference to the actual object. To also get the life-time straight you typicslly allocate the corresponding object on the stack (make sure your base class has a virtual destructor if you ever release an object of a derived type using a pointer to the base). With this, you should be all set: just call a virtual function of the base class through a pointer to rhe base: When you overridethe function in the derived class this is the function which is called.

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

Sidebar

Related Questions

All, I'm sure this is a pretty simple SQL query question, but I'm sure
This is seems like a pretty simple question, but I'm not sure if there's
This question should be pretty simple. I have a php file in a directory
I'm pretty sure this is a simple question to answer and ive seen it
This a pretty simple question, I'm just not sure how to do it exactly.
I'm pretty sure this is a simple question in regards to formatting but here's
I'm pretty sure this is a simple question, but I have no idea where
I'm pretty sure this is a rather simple and straightforward question for anyone that
Pretty simple question this time around. I have an application that communicates with another
Im pretty sure this has a simple solution. I am using jCarousellite, and i

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.