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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:42:19+00:00 2026-05-23T20:42:19+00:00

This is really two questions, noted below: Currently I have some public internal helper

  • 0

This is really two questions, noted below:

Currently I have some public internal helper structs (strictly used to pass data around as one object), during construction of an instance of the class I attempted to use initialization lists instead of assignment, but the compiler complained about the individual struct members so I added constructors to the structs…but that just seems like I’m headed down the wrong path.

Is there a way to initialize a struct in an initializer list without using a constructor?

Would these helpers be better suited as an external class?

 
    class Foo {

    public:
    //...
        struct Bar {
            double mass;
            std::pair<double, double> gravMod;
            std::pair<double, double> position;
            std::pair<double, double> velocity;
            bool falling;
            Bar() : mass(0.0), gravMod(std::make_pair(0.0, 0.0)), position(std::make_pair(0.0, 0.0)), velocity(std::make_pair(0.0, 0.0)), falling(false) { };
            Bar(double _mass, std::pair<double, double> _gravMod, std::pair<double, double> _position, std::pair<double, double> _velocity, bool _falling) 
             : mass(_mass), gravMod(_gravMod), position(_position), velocity(_velocity), falling(_falling) { }
            Bar(const Bar& other) 
                : mass(other.mass), gravMod(other.gravMod), position(other.position), velocity(other.velocity), falling(other.falling) { }
        };

        struct Baz {
            std::pair<double, double> acceleration;
            std::pair<double, double> force;
            Baz() : acceleration(std::make_pair(0.0, 0.0)), force(std::make_pair(0.0, 0.0)) { }
            Baz(std::pair<double, double> _acceleration, std::pair<double, double> _force) 
                : acceleration(_acceleration), force(_force) { }
            Baz(const Baz& other) : acceleration(other.acceleration), force(other.force) { }
        };

    //...
    protected:
    //...
    private:
        Bar _currBar;
        Bar _prevBar;
        Baz _currBaz;
        Baz _prevBaz;
    };

 

EDIT

Examples and their associated errors:

 
Foo::Foo() : _currBar{0.0, std::make_pair(0.0, 0.0), std::make_pair(0.0, 0.0), std::make_pair(0.0, 0.0), false}, _currBaz{std::make_pair(0.0, 0.0), std::make_pair(0.0, 0.0)} { }
 

_currBar{ throws: expected '('. The first } throws: expected';'.

 
Foo::Foo() : _currBar.mass(0.0), _currBar.gravMod(std::make_pair(0.0, 0.0)), _currBar.position(std::make_pair(0.0, 0.0)), _currBar.velocity(std::make_pair(0.0, 0.0)), _currBar.falling(false) { }
 

The first _currBar. throws: expected '('. All _currBar. afterwards throw Member 'Foo::_currBar' has already been initialized..

  • 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-23T20:42:20+00:00Added an answer on May 23, 2026 at 8:42 pm

    In C++03, there is no way to initialize individual fields of a nested struct, nor is there a way to initialize individual fields of a nested array. In C++0x (now C++11, I guess), they’ve relaxed this restriction and you can initialize nested structs with a syntax like this:

    Foo::Foo() : _currBar{fieldOneValue, fieldTwoValue, /* ... */, fieldNValue}, /* ... etc ... */
    

    Adding constructors to your nested structs is a perfectly viable option in the interim as we wait for more compilers to completely support this feature.

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

Sidebar

Related Questions

This is really two questions: Why isn't the membership operator (__contains__) ever being called?
This consists of two questions: Is MySQL's timestamp field really faster than datetime field
This is really a two part question: I'm seeing some users in the Grantee
I'm really new to JavaScript and jQuery. My question is this. I have two
I am having a really hard time getting this to work. I have two
So I have two custom complex types like this (oversimplified for this example): public
I'm really struggling with this now, been on it for ages and asked two
java.util.concurrent.ConcurrentLinkedQueue comes to mind, but is it really optimum for this two-thread scenario? I
This really seems like a bug to me, but perhaps some databinding gurus can
I have been coding classes like this: public class ReportViewModel { public string Status;

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.