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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:28:38+00:00 2026-06-08T03:28:38+00:00

Edit: Just to be clear, the struct doesn’t do anything, as in it has

  • 0

Edit: Just to be clear, the struct doesn’t do anything, as in it has no functions. I think I gave the impression that I thought using an initialiser list and leaving the body of the constructor empty was the issue at hand.

Say I’m using a struct to hold two values, and I have a constructor just so I can create an arbitrary struct like this:

struct twoValues
{
    int x;
    int y;

    twoValues(int x_, int y_):y(y_),x(x_)
    {}
};

someFunction(twoValues(1,2));

That saves me from having to do this:

twoValues anInstance;
anInstance.x=1;
anInstance.y=2;
someFunction(anInstance);

Edit: You’re all correct, I could also initialise with the following:

twoValues anInstance = {1,2};

I see nothing wrong with this but I had some feedback from a C++ test and one of the negative feedback marks was “constructors for structs that don’t do anything”. I had limited contact with the guy testing me and so never asked why.

Is it a bad thing and why? I would rather carry on doing it.

  • 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-08T03:28:40+00:00Added an answer on June 8, 2026 at 3:28 am

    It depends on what the struct is being used for. As others have said,
    the constructor means that the class is no longer a POD, and that
    aggregate initialization cannot be used for it. In particular, you
    cannot have something at namespace scope like:

    TwoValues const table[] =
    {
        { 1, 2 },
        { 3, 4 },
        // ...
    };
    

    You can have:

    TwoValues const table[] =
    {
        TwoValues(  1, 2  ),
        TwoValues(  3, 4  ),
        // ...
    };
    

    but it is more verbose, and it implies dynamic initialization, which may
    result in order of initialization issues.

    On the other hand, without the constructor, you cannot create temporary
    instances on the fly. Instead of:

    extern void f( TwoValues const& );
    //  ...
    
    f( TwoValues( 1, 2 ) );
    

    you have to write:

    extern void f( TwoValues const& );
    //  ...
    
    TwoValues tmp = { 1, 2 };
    f( tmp );
    

    If the object is dynamically allocated, it’s even worse, since you
    either have to allocate first, then initialize, or create a temporary as
    above, and then write new TwoValues( tmp ) and use the implicit copy
    constructor.

    You have to choose. Depending on what the struct is used for, one or
    the other will be preferred; on one hand, I have a lot of structs which
    are used exclusively in static tables, and intentionally don’t have a
    constructor (and contain only types which support static
    initialization), and use them a lot for configuring code. On the other
    hand, I also have a lot of structs which are internal to a class, along
    the lines of Node in a tree or a graph; these almost always have a
    constructor, to facilitate creating them on the fly. There’s no
    “correct” answer without knowing the role of the struct in your
    application.

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

Sidebar

Related Questions

EDIT: Just to make things clear, this problem was caused by a typo in
I have to edit my question just to make it clear. What I need
EDIT I've just started skimming Codd's famous 1970 paper that started it all, that
EDIT 2: I have just realised that my HTML is only displaying the artist
Edit For simplicity: I just want to make the most basic possible cursor that
Edit: just to be clearer on what I am looking to do. I have
Edit: Turns out I just had some values -switched-. I'm working with OpenGL and
Just a little edit, in case it helps anyone out who happens to stumble
Edit: I fixed the problem by just starting from scratch. Sorry to waste y'alls
** EDIT ** Nevermind, just needed to take out the parens... I get this

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.