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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:00:14+00:00 2026-05-17T19:00:14+00:00

let’s say i want to have a member variable for a pointer to std::vector

  • 0

let’s say i want to have a member variable for a pointer to std::vector but i do not want to specify what type of variable it stores. I want to access only those functions that are independant of it’s actual generic type. is this possible with c++? something like this:

class Foo{
public:
    void setVec(std::vector* someVec){
        myVec = someVec;
    };
    int getSize(){
        return myVec.size();
    };
private:
    std::vector* myVec;
};


int main(){
    Foo foo;
    vector<int> vec1;
    vector<float> vec2;
    foo.setVec(&vec1);
    cout<<foo.getSize();
    foo.setVec(&vec2);
    cout<<foo.getSize();
}

note: i do not want to template Foo and i want to use only a single instance of Foo with vectors of different type.

of course – if I could alter the class vector then i could create an untemplated baseclass

class Ivector{
    virtual int size()=0;
};

and then make the

class vector<T> : public IVector...

inherit from Ivector. but what do I do if i can’t alter the class in question and the templated class does not have such an untemplated baseclass?

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-17T19:00:14+00:00Added an answer on May 17, 2026 at 7:00 pm

    You are almost at the answer. Instead of making std::vector inherit from Ivector, create a new class:

    template <typename T>
    class IVectorImpl : public Ivector
    {
    public:
        explicit IVectorImpl(std::vector<T> * Data) : m_Data(Data){}
        std::vector<T> * m_Data;
     ...
         virtual int size() const {return m_Data->size();}
      // Implement all the Ivector functions here to call the respective functions off of m_Data
    };
    

    Now have your Foo class keep a pointer to Ivector instead of std::vector.

    Make Foo::setVec templated

    template <typename T>
    void setVec(std::vector<T> * vec)
    {
       Ivector * newVec = new IVectorImpl<T>(vec);
       delete myVec;
       myVec = newVec;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.