Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to declare multiple objects of the same type at once and initialize them immediately with the same rvalue by just one expression?

I want to declare multiple objects of the same type and initialize them with the same rvalue by just one expression; without the need to declare and initialize them by separate statements.

What I want is something like:

int a = b = 10;  // dummy-statement. This does not work.

or

int a = int b = 10; // dummy-statement. This does not work either.

instead of

int b = 10;
int a = b;

Is there a way to do that?

like image 318
RobertS supports Monica Cellio Avatar asked Feb 08 '20 17:02

RobertS supports Monica Cellio


People also ask

Can we declare multiple variables in a single line?

Do not declare more than one variable per declaration. Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values.

Can you declare multiple variables in C?

If your variables are the same type, you can define multiple variables in one declaration statement. For example: int age, reach; In this example, two variables called age and reach would be defined as integers.

How to create objects of the same type in JavaScript?

It is very easy to create a single object than to create multiple objects of the same type. To breach this hurdle, javascript has provided object constructor function. Using this function, initially, we have to create the type of the object and later on, we need to declare the properties of the object.

How to make two variables of the same type in JavaScript?

You could initially assign anything to signatureTypeName and it would compile. For example, you could have done let a: string = "", b = 4, which defeats OP's question, where he/she stated they want both variables to be the same type. By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Is there a way to declare variables on a separate line?

For web components with a lot of variables, declaring each on a separate line is a pain. This is especially true for form-based components. Alternatively, for those in denial :) stackoverflow.com/questions/34232315/… There is no real way to achieve what you want. If your only goal is to compress everything onto one line, you can do the following:


1 Answers

Technically, yes: int a = value, b = a;, or you might consider int a, b = a = value;. Without repeating identifiers, no, at least not in C; the grammar simply does not provide for it. Each ”declarator = initializer” in the grammar can declare only one object, per grammar production in C 2018 6.7.6 1 and explicit statement in 6.7.6 2: “Each declarator declares one identifier…”

As mentioned in a comment, here is a horrible way to do it in C++. I make no representations regarding C++’s rules about order of initialization in a single declaration, issues about threading, and so on. This is presented as an educational exercise only. Never use this in production code.

template<class T> class Sticky
{
private:
    static T LastInitializer;   //  To remember last explicit initializer.
    T Value;                    //  Actual value of this object.

public:
    //  Construct from explicit initializer.
    Sticky<T>(T InitialValue) : Value(InitialValue)
        { LastInitializer = InitialValue; }

    //  Construct without initializer.
    Sticky<T>() : Value(LastInitializer) {}

    //  Act as a T by returning const and non-const references to the value.
    operator const T &() const { return this->Value; }
    operator T &() { return this->Value; }
};

template<class T> T Sticky<T>::LastInitializer;

#include <iostream>

int main(void)
{
    Sticky<int> a = 3, b, c = 15, d;

    std::cout << "a = " << a << ".\n";
    std::cout << "b = " << b << ".\n";
    std::cout << "c = " << c << ".\n";
    std::cout << "d = " << d << ".\n";
    b = 4;
    std::cout << "b = " << b << ".\n";
    std::cout << "a = " << a << ".\n";
}

Output:

a = 3.
b = 3.
c = 15.
d = 15.
b = 4.
a = 3.
like image 144
Eric Postpischil Avatar answered Nov 04 '22 12:11

Eric Postpischil