CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2001
    Posts
    6

    abut constructor

    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

  2. #2
    Join Date
    May 2002
    Location
    United Kingdom
    Posts
    13

    Thumbs up


    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured