Thread: what does this statement mean??

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    what does this statement mean??

    What does the last line in the following code -
    Code:
    b* l = new b();
    do??

    Code:
    #include<stdio.h>
    #include<iostream>
    
    class a
    {
    protected:
    	int i;
    public:
    	void fun()
    	{
    		std::cout<<"whatever";
    	}
    	a()
    	{
    		i = 3;
    	}
    };
    
    class b: private a
    {
    public:
    	void out()
    	{
    		std::cout<<i;
    	}
    	b(int ii)
    	{
    		i = ii;
    	}
    	b()
    	{
    	}
    };
    
    int main()
    {
    	b bb;
    	b* l = new b();
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Declares l as a pointer to an object of class b and allocates memory for it.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    but why the paranthesis??

    Isn't it supposed to be
    Code:
    b* l = new b;
    ??

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by juice View Post
    but why the paranthesis??

    Isn't it supposed to be
    Code:
    b* l = new b;
    ??
    Same thing.
    But if you wanted to call the other constructor (which takes an int), the arguments would go inside that.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by manasij7479 View Post
    Same thing.
    But if you wanted to call the other constructor (which takes an int)
    ???

    is the statement calling a constructor?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    new always calls the constructor for the target type, unless it's a native type. declaring an object on the stack as you did in line 37 also calls the constructor.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by Elkvis View Post
    new always calls the constructor for the target type, unless it's a native type.
    am all confused man..

    New would have anyways called the constructor even if we had droped the paranthesis. What purpose do they serve??

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    they allow you to pass a list of parameters to the constructor. they are completely optional if you are using a default constructor - one that takes no parameters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  2. If statement being ignored?
    By FincH in forum C Programming
    Replies: 3
    Last Post: 04-18-2007, 01:51 PM
  3. help with an if statement
    By jjj93421 in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2004, 09:28 AM
  4. If (statement) and (statement)
    By renderstream in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2004, 05:36 PM
  5. Help with if statement
    By viryonia in forum C Programming
    Replies: 1
    Last Post: 02-11-2003, 09:52 PM