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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:26:31+00:00 2026-06-17T09:26:31+00:00

Please consider the following code : #include <iostream> using namespace std; class superclass; class

  • 0

Please consider the following code :

    #include <iostream>
    using namespace std;
    class superclass;
    class subclass;
    class subclass2;
    class superclass
    {
    public:
        unsigned int a;
        superclass **superman;

    };
    class subclass : public superclass
    {
    public:
        unsigned int b;
    };
    class subclass2 : public superclass
    {
    public:
        unsigned int b;
    };
    class runner
    {
    public:
        superclass **superman;
        runner()
        {
            *superman=new superclass[2];
            superman[0]=new subclass;
            superman[0]->a=3;
            superman[1]=new subclass2;
            superman[1]->a=4;
        }

    };
    int main() {

        runner r;
        cout<<r.superman[0]->a<<" "<<r.superman[1]->a;
        return 0;
    }

As you can see I want to create a dynamicaly alocated storage of references to a parent class each of which can then point to a child class how ever I do not know how to extract the child class out again from that array so i may access its variable b;

I have tried the following approaches but they have not worked for me and give the error
“conversion from ‘superclass*’ to non-scalar type ‘subclass’ requested” and
“conversion from ‘superclass*’ to non-scalar type ‘subclass2’ requested”

    subclass s1=r.superman[0];
    subclass2 s2=r.superman[1];

I am sure I am missing something small.

PS: I could not find a similar question, but if it exists, please redirect me,
also i would like a solution that does not require me to use vector or any inbuilt
pre-existing library class.

  • 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-17T09:26:32+00:00Added an answer on June 17, 2026 at 9:26 am

    You really want smart pointer in this case and superclass doesn’t need to have a pointer to itself. You can can store superclass pointer in vector which points to real derived class so the polymorphism still works:

    #include <memory>
    #include <vector>
    
    struct superclass
    {
    public:
      superclass() : a(0) {}
      virtual ~superclass() {}  // it's important to define virtual destructor as superclass is a base class
      int getA() const { return a; }
    
    private:
      unsigned int a;
    };
    
    class subclass : public superclass
    {
    public:
        unsigned int b;
    };
    class subclass2 : public superclass
    {
    public:
        unsigned int b;
    };
    
    class runner
    {
    public:
      std::vector<std::unique_ptr<superclass>> superman;
      runner()
      {    
          superman.emplace_back(new subclass());
          superman.emplace_back(new subclass2());
      }    
    };
    

    Then you can access it simply:

       int main() 
       {    
           runner r;
           std::cout << r.superman[0]->getA() <<" " < <r.superman[1]->getA();
           return 0;
       }
    

    Side note: hide your data if you can, access data through set/get functions, don’t declare members as public.

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

Sidebar

Related Questions

please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Consider the following code: #include <iostream> class Bar { public: void foo(bool b =
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following code: class Abase{}; class A1:public Abase{}; class A2:public A1{}; //etc
I'm confused about an aspect of polymorphism. Please consider the following code: #include <iostream>
Please consider the following code: type TFoo1 = class public procedure DoSomething1; end; TFoo2
Please consider the following code: public class MyClass extends javax.swing.JFrame { private JTree jTree;
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
Please consider the following code class VMContainer:INotifyPropertyChanged { Type t; VMContained contained; public Type

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.