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?
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.
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.
A copy constructor is a member function that initializes an object using another object of the same class.
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 toT D1; T D2; ... T Dn;
whereT
is a decl-specifier-seq and eachDi
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With