what does this statement mean??

This is a discussion on what does this statement mean?? within the C++ Programming forums, part of the General Programming Boards category; What does the last line in the following code - Code: b* l = new b(); do?? Code: #include<stdio.h> #include<iostream> ...

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

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Declares l as a pointer to an object of class b and allocates memory for it.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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

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

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    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
    2,361
    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
    384
    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
    2,361
    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, 02: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, 04:36 PM
  5. Help with if statement
    By viryonia in forum C Programming
    Replies: 1
    Last Post: 02-11-2003, 08:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21