Is There a Difference Between Instantiation with Parentheses or Without

Is there a difference between instantiation with parentheses or without?

The are exactly the same, you can compare opcode of these 2 scripts:

1 script:

$object1=new User();
$object1->name="Hello";
echo $object1->name;
class User {}

opcode:

line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
3 0 > FETCH_CLASS 4 :0 'User'
1 NEW $1 :0
2 DO_FCALL_BY_NAME 0
3 ASSIGN !0, $1
4 4 ASSIGN_OBJ !0, 'name'
5 OP_DATA 'Hello'
5 6 FETCH_OBJ_R $5 !0, 'name'
7 ECHO $5
6 8 NOP
9 > RETURN 1

2 script:

$object1=new User;
$object1->name="Hello";
echo $object1->name;
class User {}

opcode:

line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
3 0 > FETCH_CLASS 4 :0 'User'
1 NEW $1 :0
2 DO_FCALL_BY_NAME 0
3 ASSIGN !0, $1
4 4 ASSIGN_OBJ !0, 'name'
5 OP_DATA 'Hello'
5 6 FETCH_OBJ_R $5 !0, 'name'
7 ECHO $5
6 8 NOP
9 > RETURN 1

Instantiate class with or without parentheses?

This is a quirk of C++ syntax. The line

CTest t1;

declares a local variable of type CTest named t1. It implicitly calls the default constructor. On the other hand, the line

CTest t2();

Is not a variable declaration, but a local prototype of a function called t2 that takes no arguments and returns a CTest. The reason that the constructor isn't called for t2 is because there's no object being created here.

If you want to declare a local variable of object type and use the default constructor, you should omit the parentheses.

In C++11, you can alternatively say

CTest t2{};

Which does actually call the default constructor.

Hope this helps!

Instantiate a class with or without parentheses?

Any difference in performance is going to be absolutely negligible.

While both ways are fine, I personally would prefer using new Car(); because usually, a method is being called here, and function/method calls in PHP require (). Also, it's more consistent with instantiations that have parameters.

But in the end, it's down to taste. It doesn't matter which way you choose, but when you choose one, stick to it consistently!

Instancing a class - difference between with and without brackets

a is the class itself -- In python, classes are first class objects1. You can pass them around as parameters, alias them to different names (as you've done in your example) and then you can instances from any reference that you have in the current namespace.

a = myClass  # a and myClass identical at this point.  The interpretter won't care which you use.
a_instance = a() # instance of myClass

def make_instance(cls):
return cls()

another_instance = make_instance(a)
yet_another_instance = make_instance(myClass)

You see, python doesn't have any "compile time checking" because really -- there is no compile time. Python code gets interpreted at runtime. True, you can have SyntaxErrors pop up at when you import something, but that is still during runtime.

1No pun intended

Why do I need parentheses when instantiating a class?

In languages where the calling of a function/method/macro takes the form

foo(param1, param2, param3)

then it is almost universally true that, if foo were to take no parameters, the call would be

foo()

and further that, depending on the language

foo

is either incorrect, or means something else.

Now lets consider your example, expanded a bit

H1 = Human
H2 = Human
H3 = Human

In this case, nothing was called. H1, H2 and H3 have all been assigned the same reference, which is a reference to the class Human and not to any instance of that class.

H1 = Human()
H2 = Human()
H3 = Human()

In this case, the class was instantiated 3 times, that means that the __init__ function was run 3 sepearate times, and the result of each is a different instance of Human. H1, H2 and H3 now point to three different objects, all instances of Human. Since they are instances, they have a self.

Declaring a new instance of class with or without parentheses

Both will call the default parameter-less constructor. So I believe both are same.

Difference between creating object with () or without

The other answers correctly state that the parentheses version is actually a function declaration. To understand it intuitively, suppose you wrote MainGUIWindow f(); Looks more like a function, doesn't it? :)
The more interesting question is what is the difference between

MainGUIWindow* p = new MainGUIWindow;

and

MainGUIWindow* p = new MainGUIWindow();

The version with parentheses is called value-initialization, whereas the version without is called default-initialization. For non-POD classes there is no difference between the two. For POD-structs, however, value-initialization involves setting all members to 0,

my2c

Addition: In general, if some syntactic construct can be interpreted both as a declaration and something else, the compiler always resolves the ambiguity in favor of the declaration.

PHP class instantiation. To use or not to use the parentheses?

They are equivalent. If you are not coding by any code convention, use which you like better. Personally, I like to leave it out, as it is really just clutter to me.

Class instantiation with and without parantheses

In the second case, you're not instantiating the class, just giving it another name, tesla. The parentheses are required for instantiation.



Related Topics



Leave a reply



Submit