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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:53:09+00:00 2026-06-10T22:53:09+00:00

So what I want to do is extend the existing vector class in my

  • 0

So what I want to do is extend the existing vector class in my program to allow me to say this,

vector<string>* vec = new vector<string>(){"Value1","Value2"};

or

vector<string>* vec = new vector<string>({"Value1","Value2"});

or

vector<string> vec = {"Value1","Value2"};

I know I can accomplish something like this but doing this,

string temp[] = {"Value1","Value2"};
vector<string> vec(temp, temp+ sizeof(temp) /  sizeof( temp[0] ) ); 

This uses the vectors iterator constructor but can’t I remove the extra line?

I know in C# you can add whatever you want to existing things by using the partial key word like this,

public partial class ClassName
{
   ClassName(Stuff stuff)
   {

   }
   void AmazingNewMethod()
   {

   }    
}

Does C++ have a nifty trick like this somewhere?

Do I have to inherit vector and build a customVector that has a constructor that behind the scenes does the iterator constructor thing?

Maybe wrap those lines in a static Helper Function call that sets it by Reference and add it to a toolbox class somewhere?

I feel like lots of programmers have hit this problem. Are there any elegant solutions out there?

Thanks.

Edit: fixed the title to mention this is an Initializer List constructor.

  • 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-06-10T22:53:11+00:00Added an answer on June 10, 2026 at 10:53 pm

    In C++11 there would be initializer lists to suite this approach. As you’re mentioning .NET I now assume that you’re using MS Visual Studio. Microsoft does NOT implement initializer lists, therefore the easiest way to accomplish something like that would be a function that returns the vector with all the elements added.

    On the partial thing: C++ does not offer a feature in the same vein as .NET’s partial classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to extend RuntimeException to create this specific exception: class CompileLinkException extends RuntimeException
Folks I have a sealed class as follows. I want to extend this sealed
I want to extend Ion Auth to only allow certain email addresses to register.
I want to extend the IEnumerable class but only for types that can be
I want to extend the functionality of a part of a program that I'm
I have a class PlaylistTrack that I want to extend class Song. When I
Let's consider that I want to extend an existing JSF component, such as the
I want to extend the extension manager in Visual Studio 2010. I'd rather know
Is there a possibility to extend the size of already existing ext2 image? This
I am currently trying to extend an existing project. Unfortunately I am completely new

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.