difference between calling constructors?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: difference between calling constructors?

  1. #1
    Join Date
    Jul 2009
    Posts
    45

    difference between calling constructors?

    If I have a class ABC for example. What's the different between these calls?

    Code:
    class ABC{
    
      ABC(){
    
    
      }
    
    }
    ABC var1;

    ABC var2();

    ABC var3 = new ABC();
    I know var3 gets a pointer to the class ABC in heap. What about var1 and var2?


    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,196

    Re: difference between calling constructors?

    This creates an object, named var1, of type ABC:
    ABC var1;
    This declares a function, named var2, that takes no arguments and returns an object of type ABC:
    Code:
    ABC var2();
    This will result in a compile error:
    Code:
    ABC var3 = new ABC();
    You probably meant to write:
    Code:
    ABC* var3 = new ABC();
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2006
    Location
    Berlin, Germany
    Posts
    405

    Re: difference between calling constructors?

    Quote Originally Posted by cpthk View Post
    If I have a class ABC for example. What's the different between these calls?

    Code:
    class ABC{
    
      ABC(){
    
    
      }
    
    }
    You missed a semi-colon at the end of the class definition.

    Quote Originally Posted by cpthk View Post
    ABC var1;
    This creates a stack object of type ABC.


    Quote Originally Posted by cpthk View Post
    ABC var2();
    This is the declaration of a function named var2 which has no parameters an returns an ABC object by value.

    Quote Originally Posted by cpthk View Post
    ABC var3 = new ABC();
    As you said, this creates an ABC object in the free store - sometimes also called the heap, but I really don't want to get into a philosophical discussion about memory area names . Just notice that you forgot the * indicating a pointer. The correct form would be:

    Code:
    ABC * var3 = new ABC();
    And since you started it, there's also...

    Code:
    ABC * var4 = new ABC;
    Which for non-POD (plain old data) structures is basically the same thing as the third case.

  4. #4
    Join Date
    Jul 2009
    Posts
    45

    Re: difference between calling constructors?

    Code:
    ABC var2();
    does var2 call the constructor in the class?

  5. #5
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: difference between calling constructors?

    No. This is just a function declaration. ( see prev. posts ).
    you propably want
    Code:
    ABC var2;
    Kurt

  6. #6
    Join Date
    Jul 2009
    Posts
    45

    Resolved Re: difference between calling constructors?

    Quote Originally Posted by ZuK View Post
    No. This is just a function declaration. ( see prev. posts ).
    you propably want
    Code:
    ABC var2;
    Kurt
    So what if I have two constructor
    Code:
    class ABC{
        ABC(){
          ...
        }
        ABC(int i){
          ...
        }
    };
    Code:
    ABC varA;
    This calls default constructor.

    Code:
    ABC varB(1);
    This calls the constructor that takes an integer.

    Code:
    ABC varC();
    This is a varC function declaration that returns object of type ABC.

    Is this what you guys meant? Is so, varA and varB are kind of confusing, since you need "()" for constructor that takes parameters but you do not need "()" at all if constructor do not take parameters.

  7. #7
    Join Date
    Jan 2006
    Location
    Berlin, Germany
    Posts
    405

    Re: difference between calling constructors?

    Quote Originally Posted by cpthk View Post
    So what if I have two constructor
    Code:
    class ABC{
        ABC(){
          ...
        }
        ABC(int i){
          ...
        }
    };
    Code:
    ABC varA;
    This calls default constructor.

    Code:
    ABC varB(1);
    This calls the constructor that takes an integer.

    Code:
    ABC varC();
    This is a varC function declaration that returns object of type ABC.

    Is this what you guys meant? Is so, varA and varB are kind of confusing, since you need "()" for constructor that takes parameters but you do not need "()" at all if constructor do not take parameters.
    Yes, you've got it. It might be confusing at first, but you'll get used to it. That's C++ parsing.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center