Classname variable and classname variable()

Say we have a class named Base. What is the difference between the two declarations below? Thank you

1
2
  Base b;
  Base b();
1st on declares the object.
2nd on declares the function that return the object of the class Base.
MiiNiPaa showed how wrong I was.
Last edited on
There is no prototype section. YOu can declare function practically anywhere. http://coliru.stacked-crooked.com/a/0e4e297bae362f29

Actually even in your assembly there is only one call to constructor on line 39. I do not know what you think it proves, but it states that only one Base object is created.

Edit: to clarify what happens in assembly:
Line 29: save rbp value (reuired per x64 MS calling convention)
Line 31: save previous value of rsp (stack pointer) to rbp
Line 33: "allocate" memory on stack (32bytes of shadow space for future constructor call per x64 MS calling convention + 16 bytes for struct. )
Line 37: load address of what would be a B object in rax
Line 38: copy rax into rcx (it will be a this parameter for constructor per x64 MS calling convention)
Line 39: call function itself

Line 40: put main return value (0) to return register (rax)
Line 41: "deallocate" memory used previously
Line 42: restore rbp value
Line 43: return

Edit 2: As looking on unoptimised compiker output is painful, I decided to play around a little. GCC just threw out whole program even on lowest optimisation level, but I could get some results with clang:
1
2
3
4
5
6
7
8
9
10
11
main:                                   # @main
	pushq	%rax
	leaq	(%rsp), %rdi
	callq	Base::Base()
	xorl	%eax, %eax
	popq	%rdx
	retq

Base::Base():                           # @Base::Base()
	movl	$0, (%rdi)
	retq
Here you can see what happens more clearly: lines 2-4 = line 8 in original code (allocating memory, moving this pointer to the first argumnt, calling constructor), lines 5-7 = line 11 (moving return value to register, releasing memory — destructing object, returning).
(But using pushq/popq instead of subq 8 rsp/addq 8 rsp? Does this increase perfomance?)
Last edited on
Topic archived. No new replies allowed.