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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:40:26+00:00 2026-06-18T15:40:26+00:00

I have been writing C++ for a little while (java/C for a long while),

  • 0

I have been writing C++ for a little while (java/C for a long while), I am wondering if there is a trick I don’t know which can help me do the following.

vector<unsigned char> *fromArray(unsigned char data[], int length)
{
    vector<unsigned char> *ret = new vector<unsigned char >();
    while (length--)
    {
        ret->push_back(*data);
    }
    return ret;
}

And you can use it like so:

unsigned char tmp[] = {0, 1, 2, 3, 4, 5};
vector<unsigned char> *a = fromArray(tmp, sizeof(tmp));
// use `a' here

I find that pretty cumbersome – I’d like to write it all on one line

vector<unsigned char> *a = fromArray({0, 1, 2, 3, 4, 5});
// use `a' here

Is such a thing possible? I don’t have access to C++11 unfortunately (looks like its initializer_list is exactly what I want).

EDIT

Sorry I had some of the fundamentals wrong in here. I will now avoid extending std::vector. However I think the question is still valid, just that my example was a bad one.

** Potential workaround **

I could define a bunch of overloaded functions to take different numbers of arguments, eg

vector<unsigned char> *fromArray(unsigned char a)
{
    vector<unsigned char> *ret = new vector<unsigned char >();
    ret->push_back(a);
    return ret;
}

vector<unsigned char> *fromArray(unsigned char a, unsigned char b)
{
    vector<unsigned char> *ret = new vector<unsigned char >();
    ret->push_back(a);
    ret->push_back(b);
    return ret;
}

But I don’t think I will bother…

  • 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-18T15:40:27+00:00Added an answer on June 18, 2026 at 3:40 pm

    You can use a template to deduce the size of any fixed size array:

    template< class T, size_t N >
    auto_ptr<ByteArray> foo( T (&data)[N] )
    {
      return auto_ptr<ByteArray>(new ByteArray(data, N));
    }
    

    then

    unsigned char tmp[] = {0, 1, 2, 3, 4, 5};
    auto_ptr<ByteArray> a = foo(tmp);
    

    But bear in mind that auto_ptr is deprecated. Prefer unique_ptr. Also, note you should not publicly inherit from std::vector.

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

Sidebar

Related Questions

I've been programming in Java for a little while now but have never really
I have been thinking of maybe writing a little tool that logs into SO
I have been writing my scripts for FF but was hoping with little work
I have been writing a little application where Form1 opens, checks that a config
Ok, so I've been writing PHP code for a little while and understand it
I have been using rspec for a little while and recently switched style from
I have been writing linear winforms for couple of months and now I am
I have been writing a SOAP client application in C++ on Ubuntu using OpenSSL
I have been writing C++ Console/CMD-line applications for about a year now and would
I have been writing unit tests using NUnit and Moq with my Silverlight code

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.