Then why I can not call the following ? Stock a = Stock;
Stock a; //this is enough(it cals default constructor)
Stock a = Stock is interpreted as assign class type to the a object.
class name is indentifikator not object nor constructor, it is used to make new object.
in your case you have used that type once on the left side, no need to do so one more time on the right cos (compiler is not blind he see class type on the left which is enough)
or Stock a = new Stock();
a must be pointer !! (cos he is pointing to the memory on the heap)
I never know () can trigger a default constructor which can zero your data if you dont have one.
Thanks for both of you.
I guess no matter what, it is better to add () like the following then ? as if you do not have default constructor, it will zero out data; if you have, with () and without () will be the same: Stock a();
Stock a = Stock();
Stock *a = new Stock();
The reason why I ask is I vaguely remember when I take the class, the teacher told me NEVER use () because it is trying to define a new method or something.
The reason why I ask is I vaguely remember when I take the class, the teacher told me NEVER use () because it is trying to define a new method or something.
The () only works when you use new.
The first form you quote in the prev. post
Stock a();
is the one you should never use. This does look like a function decl.
VC tells me:
warning C4930: 'Stock a(void)': prototyped function not called (was a variable definition intended?)