question on constructor from tutorial


as the example

1.CRectangle rectb; // right
2. CRectangle rectb(); // wrong!

3. CRectangle rectb = new CRectangle;
4. CRectangle rectb = new CRectangle();

question 1
is 4 same as 3?

question 2
2 is wrong, buy why? I can compile codes wihtout prob. What is called when rectb() is called?

Thanks

1) Correct. Creates an object of type CRectangle, and uses the default constructor.

2) Wrong. It will compile okay, but doesn't do what you expect. It does not create a CRectangle object. Instead it is a function prototype which returns a CRectangle. Much like how int foo(); declares a function called foo.

3) Wrong because rectb is not a pointer. However assuming that was a typo.. CRectangle* rectb = new CRectangle; is correct. Creates a CRectangle object dynamically using the default constructor.

4) Wrong for same reason number 3 is wrong. But assuming that was a typo: CRectangle* rectb = new CRectangle(); is correct and does what you'd expect -- the same thing as #3.
crystal clear. thank you very much
Topic archived. No new replies allowed.