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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:39:36+00:00 2026-05-24T17:39:36+00:00

Finding the appropriate title to this question is a little difficult for me, but

  • 0

Finding the appropriate title to this question is a little difficult for me, but here is my situation. Say I have a base class and a derived class, can I pass a function that takes a parameter of type *base a type of instead *derived

class base
{
protected:
    float M_OutputValue;
    float M_InputValue;
public:
    virtual void CalculateOutput() = 0;
        /* more functions */
};

class derived: public base
{
public:
    void CalculateOutput(){ /*stuff*/};
    /* more functions */
};

And here is a function that would take the base class as an input.


void CalculateAllOutputs(base* basearray, int NumberofElements)
{
    int temp = 0;
    while(temp < NumberOfElements)
    {
         basearray[temp]->CalculateOutput();
         temp++;
    }
};

I’m and wondering if I can pass this function a pointer to an array of derived objects. From what I understand about casting pointer from derived to base, it is implicit and there isn’t much to worry about (I looked at this question for answers). My concern is that because the derived class has more members, it takes up more bytes. That would cause the members in the array to be farther apart, so when iterating through it, a larger offset would need to be added to the memory address to get to the next element. I’m wondering if the compiler takes care of these problems so that the function I’m passing the array to knows how to access the members of the derived array. I could also solve my problem with templates of there is no solution here, but I’m trying to get better at polymorphism.

Thanks.

  • 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-24T17:39:36+00:00Added an answer on May 24, 2026 at 5:39 pm

    You are right. Stay away from arrays, and preferably use

    std::vector<std::unique_ptr<base>>
    

    or if you don’t have a C++0x-ready compiler (if this is the case, then consider upgrading), you can use

    boost::ptr_vector<base>
    

    Both are vectors of pointers which automatically clean up the pointed to objects. Avoid raw pointers, and as you point out, plain arrays simply suck.

    Example of use:

    void calculate_outputs(const std::vector<std::unique_ptr<base>>& b)
    {
        std::for_each(b.begin(), b.end(), [](const std::unique_ptr<base>& x)
        {
            x->CalculateOutput();
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm finding this to be a difficult question to put into words, hence the
I'm sure this is possible but I am having trouble finding the appropriate documentation
Sorry for the long title, but it seems most descriptive for my question. Basically,
I have some trouble finding appropriate patterns for what I want to do. I
I'm trying to use auto-generated forms in symfony, but I have troubles finding good
I hope that this question has not been asked elsewhere, but I'm having difficulty
I'm finding it difficult to find a decent example on how to implement a
I'm finding this problem every now and then in my production website, and it
I have some location finding code and while I'm waiting for a read of
I have been finding some articles and post which suggest not to use the

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.