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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:42:23+00:00 2026-05-30T09:42:23+00:00

I have base class (virtual), with some children, and an array with pointers on

  • 0

I have base class (virtual), with some children, and an array with pointers on base classes. When I create new object and then add its adress to the array, its ok. But when instead of adding adress I use new class(), the PARENT function is called, not the CHILD. Example code:

class base
{
public:
    virtual int foo() {return 1;};
};

class child : public base
{
  int foo() {return 2;};
};

//somewhere in code...

vector<base*> arr;

//1. Its ok:
child one;
arr.push_back(&one);
cout<<arr[0].foo(); //Its 2

//2. Its not ok...
arr.push_back(new child())

cout<<arr[1].foo(); //Its 1...

Thanx for ay help…

EDIT:

Im sorry, it was compiler fault – Downloaded MinGW and works fine… Sorry for problem…

  • 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-30T09:42:24+00:00Added an answer on May 30, 2026 at 9:42 am

    foo is not public in child

    class child : public base
    {   
    public:
      int foo() {return 2;};
    };  
    

    also while I am here it should be

    arr[0]->foo();
    

    so

    child one;
    arr.push_back(&one);
    arr.push_back(new child());
    for(const base* b : arr)//C++11
        std::cout << b->foo() << '\n';
    

    result

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

Sidebar

Related Questions

Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
I have 4 base classes: class A { virtual SomeMethod () { <code> }
I have a question, here are two classes below: class Base{ public: virtual void
I have a base class object array into which I have typecasted many different
I have a base class and several derived classes. The derived classes use some
I have a stateless, abstract base class from which various concrete classes inherit. Some
I have some base class A , and some number of inherited classes: class
I have a base class with an optional virtual function class Base { virtual
I have a base class with a virtual method, and multiple subclasses that override
I have this code in base class protected virtual bool HasAnyStuff<TObject>(TObject obj) where TObject:class

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.