Is int a class?
Please consider below code
#include"iostream"
using namespace std;
class test{
public:
int a;
test(int x)
{
cout<<"In test Constructor"<<endl;
a = x;
}
};
int main()
{
test *obj = new test(10);// Constructor get called
int *x = new int(10); // Expecting the same if "int" is a class
return 0;
}
No,
intis not aclass, andint x = new int(10);is not valid C++ syntax.This just creates a pointer to an
intandnew int(5)is a way to initialize a pointer.No, because you allocated it with
new, notnew[]. The correct way though isint x = 5;orint x(5);– avoid dynamic allocation unless truly necessary.