Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

go through below code.

C#
class Test
{
public:
    Test()
    {
        cout<<"Constr"<<endl;
    }

    ~Test()
    {
        cout<<"Destructor"<<endl;
    }
};

int main(int argc, char* argv[])
{

    Test *test = new Test();
    Test *n = new Test;


    delete n;
    return 0;
}



For Underlined code will make any difference?

Regards,
Ranjith
Posted

Well, if you ignore that you only delete one of the new instances, no.

All you do is declare two variables, and create two separate instances of your class to assign to them. The name of the variable is irrelevant - the class can't even tell if it is going to be held in a variable, much less what it is called!
 
Share this answer
 
C#
Test *test = new Test(); //Initialize object "test" of type "Test" with empty argument list
Test *n = new Test; //Initialize object "n" of type "Test" using default constructor


...since you only have one constructor, these two statements are equivalent... If you had more than one type of constructor (i.e. those where you can pass arguments upon initialization), you would need to use the new Test(initializers) syntax, since new Test will always simply give you the default.
 
Share this answer
 
Comments
Stefan_Lang 27-Jun-14 4:55am    
I don't think so - no matter how many constructors are defined, new Test() will always be equivalent to new Test. The parenthesis are optional, unless you want to call a non-default constructor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900