In c++, I have a class, say T
. It is meaningful to construct an object as T t();
but not as T();
. Can the second version be blocked?
Asked
Active
Viewed 47 times
-1

user66081
- 420
- 7
- 15
-
blocked.. from what? from who? – Pavel May 06 '14 at 18:58
-
2`T t();` declares a function. It doesn't create a variable. To invoke the default constructor on a new object, you **must not** use parentheses. – zneak May 06 '14 at 18:58
-
`T t();` declare a function. You need `T t;` – Anycorn May 06 '14 at 18:58
1 Answers
-1
You can use
T t; // invokes T() implicitly (t object is created on stack)
T* t = new T(); // explicitly (object is created on heap, pointer t to that object is on stack)
But
T t(); // declares function t of a return type T

Nenad
- 335
- 1
- 7