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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:55:15+00:00 2026-05-16T23:55:15+00:00

Consider: struct A { A (int); A (const A &); }; struct B {

  • 0

Consider:

struct A {

 A (int);

 A (const A &);
};

struct B {

 A foo [2];

 B (const A & x, const A & y)
 : foo {x, y} /* HERE IS THE PROBLEM */
 {}
};

I was expecting this to work since I’m using C++0x support in GCC4.3, which allegedly supports initialiser lists. No joy.

I have a class A which has no default constructor. This is not negotiable. Assignment post-default is not an option.

I am trying to create B which uses A. B::foo may not be std::vector.

How can I initialise B::foo in B(...), constructing its elements exactly once?

At the moment, I am condidering replacing B with

struct B {

A foo_a;

B foo_b;

A * foo () {
assert ((&foo_b) - *&foo_a) == 1);
return &foo_a;
}

B (const A & x, const A & y) : foo_a(x), foo_b(y) {}
};

Or even using char foo [2*sizeof(A)] with placement new — YUK!

Surely there’s a proper way to do this?

  • 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-16T23:55:15+00:00Added an answer on May 16, 2026 at 11:55 pm

    Unfortunately, there really is no proper, clean way to do this. Consider it something of a language limitation that results from an awkward mixing of C++ constructors and C style arrays. The C++11 standard addresses this issue, but until then you’ll have to settle for a workaround.

    Since A has no default constructor, one possible work-around is to have an array of A* pointers, and then loop over the array and initialize each one with new. (Obviously, don’t forget to delete each item in the array in B’s destructor, or just use smart pointers.)

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

Sidebar

Related Questions

Consider the following code: struct Foo { Foo operator+(const Foo &rhs) const; // notice
Consider the following code: struct Foo { }; struct Bar { explicit Bar(const Foo&)
Consider this code, struct A {}; struct B { B(const A&) {} }; void
Consider the following code struct foo { const int txt_len; const int num_len; char
Consider the following code: struct Calc { Calc(const Arg1 & arg1, const Arg2 &
Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void
I was playing with Visual Studio and templates. Consider this code struct Foo {
Consider the following snippet: struct Base { virtual ~Base() {} virtual void Foo() const
Consider the following code: struct s { const int id; s(int _id): id(_id) {}
Consider: std::tuple<int , const A&> func (const A& a) { return std::make_tuple( 0 ,

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.