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 question is related to a previous question of mine to which I couldn't
This question is related to a previous post of mine Here . Basically, I
This question is directly related to my previous question ASP.NET AJAX Is it possible
This is kind of related to my previous question, but not really. I have
Ok, so - this is heavily related to my previous question Transforming an object
Related to this question: URL characters replacement in JSP with UrlRewrite I want to
This is related to a previous (unanswered) issue I've had with trying to catch
Related to my previous question: Call repaint from another class in Java? I'm new
Related to my previous question , is there some realistic chance to extract surveillance
Related to my previous question: PHP and Databases: Views, Functions and Stored Procedures performance

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.