简体   繁体   中英

to understanding this line of C++ pointers code

I am reading an example code about pointers but I can't understand why the developer write this:

If DD is the class we defined, why is this working?

DD *g = 0,h(ARGUMENTS_HERE);

but this isn't working (without zero,)?

DD *g = h(ARGUMENTS_HERE);

Given the minimal information provided, if I had to guess, I'd say the first line works because g is being defined as a pointer to DD and initialized to be 0, or null. The comma afterwords means that h(with arguments) is defined as a function (with arguments) that returns a DD object (not a pointer to the object, the actual object)

The second line fails because you're trying to initialize g to the return value of the function h. Since we don't know how h is actually defined, I'd guess that it either isn't defined or it is defined but not returning a pointer to DD.

You're declaring two different variables here, one pointer to a DD and one DD object. In the second example you're trying to assign h, which probably doesn't exist, or is an object that you are trying to assign to a pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM