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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:41:07+00:00 2026-05-29T03:41:07+00:00

I have a vector of pointers to derived objects insert by the user (so

  • 0

I have a vector of pointers to derived objects insert by the user (so I guess the correct term is “known only in runtime)::

vector<Person *> vect;

The derived classes are Male and Female.
I want to make an iteration on the vector to select only the Female objects and call the copyconstructor of that.
I thought 3 solutions:

  1. To use a flag;
  2. To use typeid
  3. To insert a calling to the copy constructor in the default constructor of Female so every time the user creates one, automatically create the twin.

I don’t like the first option in the case of many kind of derived classes.
I don’t like the third option too because would cause a problem of relationship (the World knows every Female but the Female can’t know the World).
So I should use the second option:
example

typeid(vect.at(i))==typeid(Female)

Is this expression correct?
Is there another way to outline the 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-29T03:41:09+00:00Added an answer on May 29, 2026 at 3:41 am

    Having Male and Female inherit from Person sounds like a really strange design, but here we go:

    vector<Person*> vect;
    vector<Female*> females;
    for (vector<Person*>::const_iterator it = vect.begin(); it != vect.end(); ++it)
    {
        if (Female* p = dynamic_cast<Female*>(*it))
        {
            females.push_back(p);   // copy the pointer
        }
    }
    

    If you really want to perform a copy of the female, which again sounds strange, replace the last line with:

            females.push_back(new Female(*p));   // copy the pointee
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of pointers to objects. I'd like to remove objects from
I have a vector of pointers to Mouse objects called 'mice'. I'm passing the
I have a class with a vector of pointers to objects. I've introduced some
I have a vector of pointers to objects. I need to remove an element
I have a vector of pointers to objects created with new . Multiple threads
I have a vector that stores pointers to many objects instantiated dynamically, and I'm
I have a class CMyVector which holds a vector of pointers to CMyClass objects
I have a set of objects in a vector of pointers to their baseclass
I have a vector of pointers to a class. I need to call their
I have a vector of pointers. I would like to call a function for

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.