tags:

views:

292

answers:

5

For some class C:

C* a = new C();

C* b(a);  //what does it do?

C* b = a;  //is there a difference?
+14  A: 

C* b(a) and C* b = a are equivalent. As with many languages, there's more than one way to do it...

anon
In retrospect should have used int to clear the confusion. int a = 5; int* b = int* c(I suppose these are equivalent, and both pointers b and c point to same value a.
It's a bit frightening that such a simple question could generate so many mistaken responses.
anon
They're not 100% equivalent. They're equivalent for this example because all types are the same. However, they're not equivalent where class types with converting constructors are involved. It would be good to explain this in your answer (given that it's been selected as correct).
Richard Corden
In the case the questioner asked about, they are 100% equivalent. If you want to post an alternative answer, feel free.
anon
I understand (and have posted). I just felt that given that your answer has been selected you might just make a note that "it's only for this specific example that this is true".
Richard Corden
It is not true "only for this specific example", it is true for all POD data types. I'm a fraid I'm a firm believer in aswering the question as asked.
anon
A: 

The first one creates a new instance of C and puts its address in a.

The second one is a pointer-to-function declaration. This pointer can point to any function taking an argument of type a and returns a pointer to an object of type C.

The third one declares b, a pointer to an object of type C and initializes it with a.

M. Jahedbozorgan
I suppose one out of three correct is npt so bad.
anon
Which one is correct? ;)
M. Jahedbozorgan
The first one is correct.
anon
@Neil I find the third one correct, except maybe for a missing comma (,) after "C".
Daniel Daranas
@daniel - nope, there is no assignment involved, only initialisation - it's important to differentiate between the two.
anon
@Neil, ok, you're right. I think this is a mistake that I may do sometimes in common language, since I didn't detect it at first sight. "int i=8;" doesn't declare an integer "i" and assign 8 to it, instead it initializes it with 8.
Daniel Daranas
You are right, assiging is different from initializing.
M. Jahedbozorgan
A: 
  1. C* a = new C(); Now it is creating a pointer of type C, which also allocate new memory by using new keyword....
  2. Following statement is depend upon your constructor logic. C* b(a); //what does it do?
  3. Your first and third statement are equivalent. C* b = a; //is there a difference?
Syed Tayyab Ali
1. C* a = new C() is perfectly vald C++ syntax. 2. Huh? 3. Correct.
anon
Yes, it is all depend on your constructor logic, if you are copying a into b, then all three same, but the information your provided, it seems that 1 and three are perfectly pointing to same location in the memory.
Syed Tayyab Ali
any ways, i edit it and read.
Syed Tayyab Ali
I didn't downvote it, but your first and second points are both wrong - the original syntax is correct and there is no constructor involved in C * b(a).
anon
@Syed, it was me. I normally don't downvote because it costs me one point but in this case it was urgent to make it clear that almost all the answers but Neil's were wrong and thus, if taken together, might have a misleading effect.
Daniel Daranas
If we were talking about C a; C b(a);, then there would be a copy constructor involved. We're talking about pointers, though, not objects.
David Thornley
That I was talking that 1 and three are absolutely pointing at same memory location. But 2nd one is depend upon constructor logic, thus there is still doubt for 2nd.
Syed Tayyab Ali
@Syed, b is a pointer. A pointer in C++ doesn't have a constructor, it's very similar (physically speaking) to a long. It's basically a number; an address. "C * b(a);" is similar to "long m(n)". There's no copy constructor involved. If n had a value of 14560, then m will have a value of 14560. Similarly, if "a" had a value of 0x45000001h, then "b" will have a value of 0x45000001h. That's why your answer about constructor logic is incorrect.
Daniel Daranas
I got it now....Thanks @Danel Daranas
Syed Tayyab Ali
@Syed, great! you're welcome.
Daniel Daranas
+4  A: 

Note that in

C* a = new C();
C* b(a);

b is a pointer to a C object assigned the same value as a. However,

#include "somefile.h"
C* b(a);

we could just as easily be defining b as a function which takes an object of type a, and returns a pointer to C.

James Curran
+3  A: 

The standard describes the different kinds of initialization is 8.5, and these two specifically under 8.5/12.

C* b(a);  //what does it do?

This is called direct initialization. If 'b' had class type, then the compiler would perform overload resolution on the constructors in C using 'a' as an argument. For a pointer type, it simply initializes 'b' with 'a'.

C* b = a;  //is there a difference?

The standard does consider these to be different in some cases, the above syntax is called copy initialization. As for direct initialization as 'b' is not a class type, then it is initialized with the value of 'a'. If 'a' and 'b' are the same class type, then direct initialization is used.

Where 'b' is a class type and 'a' has a different type (class or not) then the rules are slightly different (8.5/14-b1.b3). So for the following code:

C b = a;

Firstly, an attempt is made to convert 'a' to type 'C' and then this temporary object is used to initialize 'b'. This is significant as you can have a situation where direct initialization succeeds but copy initialization fails:

class A {
public:
  operator int ();
};

class B {
public:
  B (int);
};

void foo ()
{
  A a;
  B b1 (a);  // Succeeds
  B b2 = a;  // Fails
}
Richard Corden