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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:33:25+00:00 2026-06-08T07:33:25+00:00

Suppose I have a structure (or a class that I use just for storing)

  • 0

Suppose I have a structure (or a class that I use just for storing) filled with doubless. I make a constructor to assign to those doubless:

struct Point {
  double time;
  double x;
  double y;
  Point(double a_time, double a_x, double a_y)
    : time(a_time), x(a_x), y(a_y) {}
}

It seems redundant to declare members in the class if they are already declared in the constructor.
Is there a way to reduce these redundancy so that members that are used in the constructor are automatically public members of the class? also might be useful for other member functions.

this example is similar to the one on page 348 of Programming: Principles and Practice Using C++. So this is contemporary established style of coding.
Furthermore, I’m looking for a modern C++ style solution, meaning without pre-processor.

Update (1)
this struct needs to be used in the following way:

vector<Point> points
points.push_back(t0,x0,y0)
  • 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-08T07:33:26+00:00Added an answer on June 8, 2026 at 7:33 am

    The way you have it is perfectly fine. However, if you want to just give default values, in C++11 you can initialize your members directly and you don’t need a constructor:

    struct Point {
      double time = 0.0;
      double x = 0.0;
      double y = 0.0;
    };
    

    Also, it looks like this struct is just a bag of data, if that is the case you can also use aggregate initialization which works without C++11:

    Example 1

    struct Point {
      double time;
      double x;
      double y;
    } p = {0.0, 0.0, 0.0};
    

    Example 2

    struct Point {
      double time;
      double x;
      double y;
    };
    
    Point p1 = {0.0, 0.0, 0.0};
    

    After update

    You can use push_back like this with a flat data type:

    v.push_back({t0, x0, y0});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a structure in C or C++, such as: struct ConfigurableElement {
Suppose i have this structure of elements: <div class=parent> <div class=something1> <div class=something2> <div
Suppose I have code that maintains a parent/children structure. In such a structure I
Suppose I have a CommonClass class that displays what is common to all others
I have a class that I am monitoring using god. This is it's structure:
Suppose I have a class structure like the following, where I have a temporary
Suppose I have the following class structure: public class State { public int Id
suppose we have this class structure: public class BaseClass { public BaseClass() { Console.WriteLine(Base
I have a data structure that represents C# code like this: class Namespace: string
Suppose I have a structure in C++ containing a name and a number, e.g.

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.