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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:42:07+00:00 2026-05-31T18:42:07+00:00

What I have: Vectors of different custom structs(one custom struct per vector) I used

  • 0

What I have:
Vectors of different custom structs(one custom struct per vector)
I used pointers to each struct to give it a static size per record
A vector that combines these vectors via a pointer to each
When I try to cast a pointer to the custom vector it fails on every iteration
My workaround is a call to a function that takes the vector pointer as an argument and returns a void pointer.
I know this is wrong despite being functional, but I can’t find a reference on the right way to define the cast method properly.

I’m looking for the right way to accomplish this.

typedef struct mystruct {
    DWORD something;
    vectorclassa * somethinga;
    vectorclassb * somethingb;
    };

typedef std::vector<mystruct*> amystruct;
void * theWrongWay(mystruct * apointer){
    return apointer;
}

typedef std::vector bigvector;

If I try to bigvector.push_back(&instance_of_amystruct)
It fails. If I change std::vector<amystruct*> bigvector to a void* and call theWrongWay with a &amystruct instance, it compiles/runs. It just seems wrong.

The problem is that I don’t know how to define the missing method for vector or cast it to something vector knows how to deal with without doing something…bad.

  • 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-31T18:42:08+00:00Added an answer on May 31, 2026 at 6:42 pm

    It’s very hard to answer this question because it’s hard to tell why you want to do this. Why do you want to keep vectors of different types in another vector?

    Anyway, I’ll just assume that you want to do this because there’s some common action you want to take on each vector. Then you can simply define an abstract class that defines that action, and have a templated child class of this class that keep a different vector depending on the template argument. You can then keep the vectors in a container referring to them as their common ancestor. Some code:

    class ActionableVector {
       virtual void doSuperCoolStuff() = 0;
    }
    
    template<typename T>
    class VectorHandler: public ActionableVector {
        vector<T> handledVector;
        // vector<T> & handleVector; // You can keep a reference to an external vector too
        virtual void doSuperCoolStuff() {
            //do super cool stuff in a type-safe manner
        }
    }
    
    template<>
    class VectorHandler<ATypeThatNeedsSpecialAttention>: public ActionableVector {
        Vector<ATypeThatNeedsSpecialAttention> handledVector;
        virtual void doSuperCoolStuff() {
             // Do especially cool stuff
        }
    
    vector<ActionableVector*> myVectors;
    
    for(ActionableVector * av: myVectors ) { //C++ 11 yaay
        av->doSuperCoolStuff();
    }
    

    If you really really really want to keep objects of completely different types in a container, and willing to sell your soul to the devil for that, look at this answer.

    • 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 custom Struct that needs to be sorted on different
Lets say I have vectors of different objects, say D is of type vector<
I have two vectors in R of different size, and I want to add
Say I have two 2D vectors, one for an objects current position and one
I have a vector of vectors: vector< vector<int> > BigVec; It contains an arbitrary
I have a function that I use to add vectors, like this: public static
So, I have a cell-array of 1xN vectors of different lengths. I want to
I'm making a custom ListCellRenderer. I know that you can have different dimensions for
I have two vector objects that contain different types of data that are ordered
First, I assume each structure-specific sequences would have different ways to remove an item:

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.