answersLogoWhite

0


Best Answer

Consider the following structure:

struct X {

int a;

double b;

// ...

};

Here we could initialise with a std::tuple<int, double>. To achieve this we simply define a constructor that accepts the required tuple:

#include<tuple>

struct X {

int a;

double b;

X::X (std::tuple<int, double>& t): a {std::get<0>(t)}, b {std::get<1>(t)} {}

// ...

};

Note that any constructor that has one argument is known as a conversion constructor, in this case converting from tuple to X. It is usually a good idea to declare such constructors explicit, particularly if you also provide the complementary conversion operator (from X to tuple).

#include<tuple>

struct X {

int a;

double b;

explicit X::X (const std::tuple<int, double>& t): a {std::get<0>(t)}, b {std::get<1>(t)} {} operator std::tuple<int, double> (void) const {return std::make_tuple (a, b);}

// ...

};

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do we tuple initialize a class or structure?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the most widely used database structure?

The table or tuple.


What is tuple variables in dbms?

TUPLE : Is the "ROW" in a table


Is object a member of a class?

Yes, you would need to define your variables. Also initialize them


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


What is the difference between initialize of elements in a class with the use of constructor and without use of it?

when initializing a class elements by using constructor it will assigned to the elements when object creation is going on. by using other ways the elements will be initialize with default values when object creation


What is conustrutor?

A constructor is a function in C which has the same name of the class. The constructor can be used to initialize some function.


What function will be called by default to initialize the member variables of the object of the class?

The constructor. It's run each time a new object is created, usually setup to initialize member variables, but it can do most anything.


What is meant by a database row What is a tuple?

In database there are no. of records stored in it. These records are stored in table . Row in this table is known as a tuple. So tuple is basically a row.


How is a structure initialized?

In C, structures are uninitialized by default. To initialize a structure you will typically zero the memory allocated to the structure and then set specific members to specific values. If all members are non-zero, you can simply set those members rather than zero the memory first. In C++, structures are initialized via inline initializes and/or through the class constructor.


How do you initialize a structure?

struct Details { string name; int age; char gender; } Stud{"Phumlani", 21, "M"};


Default value of register variable?

If you don't initialize it, you will find random garbage in it. (The same is true for auto class.)


What is the purpose of constructor in object oriented programming?

A constructor in a class is one of the first pieces of code to be executed when you instantiate a class. The purpose of constructors is to have code that initialize the class and prepare the variables that may be required by the class...