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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:05:23+00:00 2026-05-29T07:05:23+00:00

I have a math vector class that is designed as follows: class Vector3D {

  • 0

I have a math vector class that is designed as follows:

class Vector3D {
public:
    float x;
    float y;
    float z;

public:
    Vector3D() {}
    Vector3D(float xx, float yy, float zz = 0.0) { x=xx; y=yy; z=zz; }
    Vector3D(const float v[]) { x=v[0]; y=v[1]; z=v[2]; }
    Vector3D(const Vector3D& v) { x=v.x; y=v.y; z=v.z; }

    // math member methods
    // ...
};

I used to use the following to create a Vector3D-type variable on the stack:

Vector3D vec1 = Vector3D(1.0, 1.0, 1.0);

I heard a can shorten this up with C++0x by implementing an initializer list constructor, so it will be possible to write something like:

Vector3D vec1 = { 1.0, 1.0, 1.0 };

What is the right way to implement this?

Update

The curly braces syntax really works out of the box for this class! Thanks for the answer and the comments!

Also, I did some synthetic performance tests trying to measure if constructor initializer list gives a speedup over member variable assignment in a constructor. Below is the results I’ve got with g++ 4.6.1:

  1. As is (member assignment in a constructor & custom copy constructor):

    Median:       634860 ns
    Median, CPI:  15.8715 ns
    Average:      636614 ns
    Average, CPI: 15.9154 ns 
    
  2. Using constructor initializer list & custom copy constructor:

    Median:       634928 ns
    Median, CPI:  15.8732 ns
    Average:      636312 ns
    Average, CPI: 15.9078 ns
    
  3. Using constructor initializer list & default copy constructor:

    Median:       860337 ns
    Median, CPI:  21.5084 ns
    Average:      864391 ns
    Average, CPI: 21.6098 ns
    

Some of the conclusions:

  • The constructor initializer list does not give a speedup over member variable assignment in the case of the math vector class presented above.
  • The custom copy constructor is more than 35% faster than the default copy 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-05-29T07:05:23+00:00Added an answer on May 29, 2026 at 7:05 am

    Brace-initialization works for all sorts of constructors, and you don’t need an initializer-list constructor argument in this case. On the contrary, initializer lists are for variable content, like the content of a dynamic container, but not for fixed-length constructor arguments. So you can just say:

    vector3D v { 1, 1, 1 };
    

    and all will be fine.

    Note however that you should really initialize your class members rather than assigning them:

    Vector3D(float xx, float yy, float zz = 0.0) : x(xx), y(yy), z(zz) { }
    Vector3D(const float v[]) : x(v[0]), y(v[1]), z(v[2]) { }
    

    You also shouldn’t write a copy constructor, since it does no better then the one that’s provided by default. The same goes for the assignment operator.

    (Personally, I don’t feel comfortable with the float[] constructor; it’d be better to use a std::array<float, 3>; but then again you might just use such an array as your 3D-vector type from the start and not bother writing a custom class at all.)

    Finally, you can combine construct-initializer-lists and initializer-list-constructors in this last example of making a list of vectors:

    std::list<Vector3D> l { { 1.0, 2.0, 3.0}, { 1.5, 3.0, 4.4 }, { 0.0, -1.2, 4.1 } };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template Vector class for 3D vector math. I'd like to have
I have a vector class in C# (a fragment below). My issue is that
I have thos piece of code: Math&&Math.random?Math.floor(Math.random()*10000000000000):Date.getTime(); And as far as i know &&
I have a method that accepts an IEnumerable-decimals and performance various math functions. I
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have a problem due to my terrible math abilities, that I cannot figure
So I have this code: #include boost_bind.h #include <math.h> #include <vector> #include <algorithm> double
In a game that I am writing, I use a 2D vector class which
I have the following the class: //Rectangle.h #include Point.h class Rectangle{ public: Rectangle(); void
I have a C library with numerous math routines for dealing with vectors, matrices,

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.