Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is int (built in data types) a class in c++? [closed]

Tags:

c++

class

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;
}
like image 206
Astro - Amit Avatar asked Nov 20 '25 01:11

Astro - Amit


1 Answers

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.

like image 152
Luchian Grigore Avatar answered Nov 21 '25 15:11

Luchian Grigore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!