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; }

22nd Oct 2018, 3:25 AM
Umar Farooq Anwar
Umar Farooq Anwar - avatar
2 ответов
+ 1
You created 2 objects so the constructor is called twice, the last one index is out of bound.
22nd Oct 2018, 3:36 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
- 1
This is a common mistake by beginners. a[2].print() is impossible. Lol
22nd Oct 2018, 7:08 AM
Kuyondo
Kuyondo - avatar