Click to See Complete Forum and Search --> : abut constructor


Manorama
May 28th, 2002, 04:30 AM
SomeClass* psc = new SomeClass();
SomeClass *psc = new SomeClass;
Given that the class SomeClass has a user-declared default constructor, what is the difference between the two statements above?

thanks in advance

vikramn
May 28th, 2002, 06:23 AM
:eek:
It looks like both the statements are same- they call the default constructor.(constructor without parameters)

If you have a class with the following constructor

SomeClass(int i)
{
...
}

and have a calls like these

SomeClass* psc1 = new SomeClass(10);
SomeClass *psc2 = new SomeClass;

the 2nd line fails to compile as there is no default constuctor avaliable.

BUT If you have the following constuctor

SomeClass()
{
...
}

and have a calls like these

SomeClass* psc1 = new SomeClass();
SomeClass *psc2 = new SomeClass;

The file complies and you can use both the pointers.

The same happens if you remove your own default constructor as the compiler provides one.

Hope this helps

Cheers
V :D