Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If two objects are declared in a single line, in which order are they constructed?

Let's say a class has been defined as

class A { //..... }; 

and now I am creating two objects as

A a,b; 

In what order are a and b created? Is it defined by the standard?

like image 308
pasha Avatar asked Feb 22 '16 18:02

pasha


People also ask

At which line is an object created?

The first line creates an object of the Point class, and the second and third lines each create an object of the Rectangle class. Each of these statements has three parts (discussed in detail below): Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.

How are objects constructed in Java?

Creating an Object In Java, the new keyword is used to create new objects. Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object. Initialization − The 'new' keyword is followed by a call to a constructor.

Which constructor is used to declare and initialize an object from another object?

A copy constructor is a member function that initializes an object using another object of the same class.


1 Answers

From 8 Declarators [dcl.decl] 3:

Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.

It goes on to say

A declaration with several declarators is usually equivalent to the corresponding sequence of declarations each with a single declarator. That is T D1, D2, ... Dn; is usually equivalent to T D1; T D2; ... T Dn; where T is a decl-specifier-seq and each Di is an init-declarator. An exception occurs when a name introduced by one of the declarators hides a type name used by the decl-specifiers, so that when the same decl-specifiers are used in a subsequent declaration, they do not have the same meaning.

You can say that they are constructed from left to right.

like image 85
erip Avatar answered Sep 21 '22 23:09

erip