Constructors for Structs in C++
NickName:IntegrateThis Ask DateTime:2016-10-05T11:15:02

Constructors for Structs in C++

I came across the following practice question and answer while studying C++ and I do not understand it.

Given:

class B {};

struct A {
   A( B b );
};

Call the function void test( A a, int* b=0); with the two corresponding variables B b, int i;

The answer is test( b, &i );

My question is, how is it enough to pass the necessary parameter of the constructor and not actually call it? In my mind, the answer should have been:

test( A(b), &i);

Copyright Notice:Content Author:「IntegrateThis」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39864867/constructors-for-structs-in-c

More about “Constructors for Structs in C++” related questions

Parameterless constructors in structs for C# 6

My understanding is that Parameterless constructors in structs are now allowed. But the following gives me a compile error in VS 2015 Community public struct Person { public string Name { g...

Show Detail

Structs and Cuda C

I don't recall being able to add constructors and overloaded operators to structs in plain C. In a book I'm reading it uses both in an example of CUDA C. If there are any differences between str...

Show Detail

structs in C# and constructors

I have data that I would ideally want to represent as so: LinkedList<T>[] However, you cannot do that on generics, so I wrapped it in a struct: public struct SuitList ...

Show Detail

Implicit List Initialization Constructors for Structs

GCC 5.4 compiles this without any warnings (using -std=c++11 -Wall -pedantic): #include <iostream> struct Coord { double x, y; }; Coord origin() { return {0.0, 0.0}; } int

Show Detail

Structs cannot contain explicit parameterless constructors

I'd like to define a struct that includes StopWatch and then an array of struct. struct SWExecutionTime { public Stopwatch swExeTime; public int intSWRecCount; pub...

Show Detail

Initialise C-structs in C++

I am creating a bunch of C structs so i can encapsulate data to be passed over a dll c interface. The structs have many members, and I want them to have defaults, so that they can be created with o...

Show Detail

Default and brace constructors for structs with default-valued members

I’d like to know if there is some difference in specifying C++ default constructors for structs in situations in which default values are specified for some of the members: struct Astruct { std::

Show Detail

Generate C struct constructors automatically?

I want to implement a new language, and I would like to do it in C, with the famous flex+yacc combination. Well, the thing is, writing the whole AST code is very time consuming. Is there a tool that

Show Detail

Constructors for structs that don't do anything

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 emp...

Show Detail

Struct constructors and layout

[Edited and expanded based on answers and comments] We have a public C# API that uses nested structs without any constructors. Note: public means its not ours and we need to communicate with it as...

Show Detail