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, int is not a class, and int x = new int(10); is not valid C++ syntax.
int* x = new int(5);
...
...
delete x;
This just creates a pointer to an int and new int(5) is a way to initialize a pointer.
And the correct way should be delete[] x;
No, because you allocated it with new, not new[]. The correct way though is int x = 5; or int x(5); - avoid dynamic allocation unless truly necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With