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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:03:55+00:00 2026-05-23T02:03:55+00:00

I have an abstract base class and 2 different classes that implements a virtual

  • 0

I have an abstract base class and 2 different classes that implements a virtual function from the baseclass.

I put these in an array, and for “derived1” class this works.
If I however make an array of “derived2” class that has some extra private variables, the code will compile but errors at runtime.

#include <iostream>
class base{
protected:
  int inner_a;
  int inner_b;
public:
  void setInner(int a,int b){inner_a=a;inner_b=b;};
  virtual int doStuff()=0;
};


class derived1: public base{
public:
  virtual int doStuff();
};



class derived2: public base{
private:
  int tmpVar;//works if I remove
public:
  int doStuff();
};


int derived2::doStuff(){

  return inner_a-inner_b;
}

int derived1::doStuff(){
    return inner_a+inner_b;
}



int main(){
  base *classAry1 = new derived1[3];//this works
  base *classAry2 = new derived2[2];//derived2 has extra private variables


  classAry1[0].setInner(1,3);
  classAry1[1].setInner(10,7);
  std::cout <<classAry1[0].doStuff() <<std::endl;;
  std::cout <<classAry1[1].doStuff() <<std::endl;


  classAry2[0].setInner(1,3);
  classAry2[1].setInner(10,7);
  std::cout <<classAry2[0].doStuff() <<std::endl;;
  std::cout <<classAry2[1].doStuff() <<std::endl;


  return 0;
}

Can anyone help me, on how to put deriveded classes in a std array?

Thanks

edit:

The code segfaults, and valgrind tells me

-2

==25096== Use of uninitialised value of size 8
==25096==    at 0x400AC9: main (abc.cpp:52)
==25096== 
==25096== Invalid read of size 8
==25096==    at 0x400AC9: main (abc.cpp:52)
==25096==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==25096== 
==25096== 
==25096== Process terminating with default action of signal 11 (SIGSEGV)
==25096==  Access not within mapped region at address 0x0
==25096==    at 0x400AC9: main (abc.cpp:52)
  • 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-23T02:03:57+00:00Added an answer on May 23, 2026 at 2:03 am

    An array of derived1 objects (or derived2 objects) cannot be interpreted as an array of base objects. Arrays are not polymorphic. Only standalone object can be polymorphic, i.e. derived1 has IS-A relationship with base. But array of derived1 does not have IS-A relationship with array of base. Neither of your arrays (neither classAry1 nor classAry2) can really “work”.

    In other words, this

    base *classAry1 = new derived1[3];
    base *classAry2 = new derived2[2];
    

    already does not make any sense, even though it is formally well-formed code.

    The first array “appears to work” just by pure accident. The behavior of your code is undefined, even when you work with classAry1.

    If you want to have an array (or a container) that stores polymorphic entities, you have to store pointers to actual objects in that array, instead of storing the actual objects themselves.

    In your specific case, the code can be rewritten in the following way. (It doesn’t look very nice and I’m just doing it to illustrate the principle, since without knowing your full intent it is hard to choose the best approach)

    int main(){
      derived1 *d1s = new derived1[2];
      base **classAry1 = new base *[2];
      classAry1[0] = &d1s[0];
      classAry1[1] = &d1s[1];
    
      derived2 *d2s = new derived2[2];
      base **classAry2 = new base *[2];
      classAry2[0] = &d2s[0];
      classAry2[1] = &d2s[1];
    
      classAry1[0]->setInner(1,3);
      classAry1[1]->setInner(10,7);
      std::cout << classAry1[0]->doStuff() << std::endl;;
      std::cout << classAry1[1]->doStuff() << std::endl;
    
      classAry2[0]->setInner(1,3);
      classAry2[1]->setInner(10,7);
      std::cout << classAry2[0]->doStuff() << std::endl;;
      std::cout << classAry2[1]->doStuff() << std::endl;
    
      delete[] classAry2;
      delete[] d2s;
      delete[] classAry1;
      delete[] d1s;
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class that I have many inherited classes coming off
I have a base class DockedToolWindow : Form, and many classes that derive from
I have a collection of classes that inherit from an abstract class I created.
I have a set of classes that inherit from the same base class and
I have an abstract base class T , from which classes A and B
I have an abstract base class called Shape from which both Circle and Rectangle
I have a abstract base class A and a set of 10 derived classes.
Ok so I have an abstract base class called Product, a KitItem class that
I have a bunch of classes extending an abstract Base class. Each subclass takes

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.