Reputation: 6249
In which of the following is the constructor of myClass called?
1. myClass class1;
2. myClass* class1;
3. myClass* class1 = new myClass;
Thanks a lot
Upvotes: 22
Views: 18850
Reputation: 13690
But this code will not instantiate any object, as it does not compile. ;-) Try this one:
myClass class1;
myClass* class2;
myClass* class3 = new myClass;
Upvotes: 6
Reputation: 18657
In both #1 and #3 since you are actually making an instance of the object. In #2 you are merely declaring a pointer that doesn't point to an instance.
Upvotes: 5
Reputation: 27613
The constructor is called in cases 1 and 3 when a class is instantiated. The other one (2) only declares a pointer.
Upvotes: 1
Reputation: 1186
Upvotes: 39