Thread: Constructor call difference

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    20

    Constructor call difference

    Hi

    I have that test class

    Code:
    #include<iostream>
    
    class TestClass
    {
    public:
    	TestClass()
    	{
    		std::cout << "Constructor" <<std::endl;
    	}
    
    
    	~TestClass()
    	{
    		std::cout << "Deconstructor" << std::endl;
    	}
    };
    
    
    int main()
    {
    
    	//what is the difference between them 
    	TestClass rt; // show constructor, deconstructor output
    	TestClass rt1(); // does nothing
    
    	return 1;
    }
    I am interested what is the difference between the two object declaration, because second declaration ( TestClass rt1(); ) seems to do nothing


    Thanks

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    TestClass rt1(); // does nothing

    Because the compiler thinks this is a function declaration

    rt1() is a function that takes no parameters and returns a TestClass.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the 2nd one looks like a prototype for a function called rt1, taking no parameters and returning a TestClass
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Incidentally, the more usual word for what you're calling "deconstructor" is "destructor".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM