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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:09:28+00:00 2026-06-07T16:09:28+00:00

For example, I have class A: class A{ int value_; public: A(A& a){ value_

  • 0

For example, I have class A:

 class A{
           int value_;
           public:
           A(A& a){
                value_ = a.value_;
           }
           A(int value){
                value_ = value;
           }
 };

I want a vector of class A but I’d like to pass a value to A(int value) for all of them.

 std::vector<A,allocator<A>> my_vector;
  • What is the best way to do it?
  • Is there a way by using allocator?
  • 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-07T16:09:30+00:00Added an answer on June 7, 2026 at 4:09 pm

    c++11

    With the new standard added functionality was granted to objects of Allocator type.

    One of the features added was that Allocators now allows emplacement construction, aka. construction of objects using a constructor other than copy/move.


    template< class U, class... Args > void construct( U* p, Args&&... args );
    

    The standard does guarantee that STL containers must use this new feature, and with that said you could implement your own allocator just for the purpose of default initializing a non-default-initializable object.


    It’s not the prettiest solution, but whatever floats your boat..

    c++03

    The allocator has nothing to do with that part of object initialization, it’s only purpose is to allocate/deallocate memory, the type of initialization you are referring to is done elsewhere.

    The only constructor an allocator will call is the copy-constructor when someone asks it to perform a placement-new, and the value passed to that copy-ctor has already been established somewhere else.

    To sum things up; No, you cannot use an allocator so solve this particular problem.


    when does std::vector require the use of a default-ctor?

    std::vector only uses the default-constructor of the type it holds in two situations:

    1. You specify the number of elements of your std::vector in the appropriate constructor overload but doesn’t supply a default value

    2. You use std::vector<T>::resize (n) and increase the number of objects in the container (note the lack of specifying the 2nd argument to the member-function)


    With the above in mind we can do plenty of things using the container without supplying a default constructor in our object, like initializing it to contain N elements of value X.

    struct A{
      A (A const& a)
       : value_ (a.value_) 
      { } 
    
      A (int value)
        : value_ (value)
      {}  
    
      int value_;
    };
    

    int
    main (int argc, char *argv[])
    {
      std::vector<A> vec (5, A(1)); // initialize vector with 5 elements of A(1)
    
      vec.push_back (A(3));         // add another element
    }
    

    But I really want to be able to use vec.resize ()!?

    Then you have two, three, four options:

    1. Go with the C++11 approach of using Allocators

    2. make your object have a default constructor

    3. wrap your object with a very thin wrapper who’s only purpose is to default initialize the containing object (this might be easier said then done in some circumstances)

    4. "Wrapping [the object] in boost::optional practically gives any type a default ctor" – @Xeo

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

Sidebar

Related Questions

I have class like this class variable { public: variable(int _type=0) : type(_type), value(NULL),
For example: I have an implementation (say doSomething()) in Base class. I want all
For example can I have class Dog { int legs; Bone chewy; Bone squeeky;
For example, i have this: class BasePacket { int header; int type; } class
Example I have a repository class (DAL): public class MyRepository : IMyRepository { public
Example-I have a person class Public Class Person Private _fname As String Public Property
I have an example class containing two data points: public enum Sort { First,
for example I have the following: I want to go through each div class
For example is it better to have: public class Case : EntityIdentifiable { public
I have used singleton calss following the example: singleton class But i get the

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.