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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:33:24+00:00 2026-05-18T06:33:24+00:00

(Related to a previous unanswered question I asked). I want to implement a function

  • 0

(Related to a previous unanswered question I asked). I want to implement a function which can be called only with vectors of related classes as parameter.

For eq

if we have

class A;
class B: public A;
class C: public A;
class D

then it should be possible to call function with vector<A*>,vector<B*> or 
vector <C*> but not vector <D*>

Any suggestions

  • 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-18T06:33:25+00:00Added an answer on May 18, 2026 at 6:33 am

    I guess you already tried to create a method like

    void doSomething(std::vector<A*>& things)
    {
    }
    

    and tried do pass

    std::vector<B*> bList = ...;
    doSomething(bList);
    

    right?

    Why does the compiler complain? Because it would not make sense. Consider that doSomething() tries to do

    things.push_back(new C());
    

    This cannot work as “things” is actually a std::vector<B*>. However, if it were std::vector<A*>, push_back would work.

    So what can we learn from this? What you’re trying only makes sense if you only read from the vector, however, vector is not a read-only container.

    A simple wrapper shows a possible solution (the wrapper can be adjusted to your needs, of course). However, I have to point out that the use of virtual methods might lead to performance penalties.

    class A {};
    class B : public A {};
    
    template <class Base>
    class AbstractVectorWrapper
    {
    public:
        virtual size_t size() const = 0;
        virtual Base* getElementAt(int index) const = 0;
    };
    template <class Base, class Derived>
    class VectorWrapper : public AbstractVectorWrapper<Base>
    {
    private:
        std::vector<Derived*> m_vector;
    public:
        explicit VectorWrapper(std::vector<Derived*> const& vector)
            : m_vector(vector)
        {
        }
        virtual size_t size() const
        {
            return m_vector.size();
        }
        virtual Base* getElementAt(int index) const
        {
            return m_vector[index];
        }
    };
    
    void doSomething(AbstractVectorWrapper<A> const& wrapper)
    {
        for (size_t i=0;i<wrapper.size();i++)
        {
            A* a = wrapper.getElementAt(i);
        }
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        std::vector<A*> as;
        std::vector<B*> bs;
        doSomething(VectorWrapper<A,B>(bs));
        doSomething(VectorWrapper<A,A>(as));
    
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is related to my previous question here . I want 4 divs (absolute
Related to my previous question ( found here ), I want to be able
This is related to a previous question I asked with Jquery. If I have
This is closely related to a previous question i asked. I have a many-to-many
this is related to: a previous question BUT the question is my code only
[Disclosure: This question is slightly related to my previous question .] As you can
This is related to my previous question of ' How can I clear my
This is related to my previous question but i can't able to get solution
Related to my previous question , I have a string on the following format:
This is related to my previous question I'm solving UVA's Edit Step Ladders and

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.