0
I create an array obj[2] of 3 objects but constructor is 2 times called only,please guide me
#include<iostream> using namespace std; class abc{ public: abc(){ cout<<"called"<<endl; } void print(){ cout<<"hello"<<endl; } }; int main(){ abc a[2]; a[0].print(); a[1].print(); a[2].print(); return 0; }
2 odpowiedzi
+ 1
You created 2 objects so the constructor is called twice, the last one index is out of bound.
- 1
This is a common mistake by beginners.
a[2].print() is impossible. Lol