1

I found some code in the program I work with:

PWSTR myWchar = NULL;
WCHAR *p = myWchar = new WCHAR[4];

How would I read a line with two equal signs?

How is it computed?

A:

 myWchar  = new WCHAR[4];
 WCHAR *p = myWchar 

or B:

 WCHAR *p = myWchar ;
 myWchar  = new WCHAR[4];
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
user3443063
  • 1,455
  • 4
  • 23
  • 37
  • I have honestly no idea, what you're talking about. – πάντα ῥεῖ Aug 16 '23 at 13:04
  • 7
    It's option A.. – Jesper Juhl Aug 16 '23 at 13:04
  • 7
    Assignment is evaluated [right-to-left](https://en.cppreference.com/w/cpp/language/operator_precedence) – Mestkon Aug 16 '23 at 13:04
  • 4
    Assign operator has [Right-to-left associativity](https://en.cppreference.com/w/cpp/language/operator_precedence) – Marek R Aug 16 '23 at 13:04
  • 3
    `WCHAR *p = myWchar = new WCHAR[4];` is exactly the same as `WCHAR *p = (myWchar = new WCHAR[4]);`. And note that the initialization of variables (like `PWSTR myWchar = NULL;`) is *not* assignment. It's *initialization*. – Some programmer dude Aug 16 '23 at 13:04
  • 9
    It's not a "command" it's a "statement". Using the correct term here helps not only focus us on your question, but informs how C++ works. – tadman Aug 16 '23 at 13:08
  • _@user3443063_ there's no thing like a _"command line with two equal signs"_ in your question. You should clarify what you're actually mean please. – πάντα ῥεῖ Aug 16 '23 at 13:14
  • Coding isn't usually about : how can I write the least amount of cde even if you know how it works, just write two lines. your intent is more explicit and It will really help you when you need to debug. – Pepijn Kramer Aug 16 '23 at 13:16
  • @PepijnKramer: I'm assuming this is a toy case, not intended as some sort of golfing exercise, since the "long" code you'd actually write would take the same number of lines as the OP's "compact" code: `PWSTR myWchar = new WCHAR[4];` followed by `WCHAR *p = myWchar;` – ShadowRanger Aug 16 '23 at 13:19
  • @ShadowRanger I know, but toy examples have a tendency to still be used ;) – Pepijn Kramer Aug 16 '23 at 13:21
  • Useful term: chaining – user4581301 Aug 16 '23 at 16:25
  • 1
    Just be aware of some details: e.g. `double a = 1.1; int b; double c = b = a;` the value of `c` will be `1`. I think it's *normally* best to write out longhand. – Bathsheba Aug 16 '23 at 16:31
  • This is not a *command* or a *statement*, it's a declaration with an initialization *expression*. Just like `int i = 2 + 4 * 10`. – Stephen M. Webb Aug 16 '23 at 16:58

1 Answers1

8

It's option A, exactly equivalent to (with unnecessary parens):

WCHAR *p = (myWchar = new WCHAR[4]);

If myWchar had a custom operator= and/or the type of p had a custom constructor or cast from myWchar's type to p's type, this could mean p and myWchar end up slightly different from one another, but in this case, WCHAR* and PWSTR are fundamentally the same type, so they both end up assigned to the same thing, the result of the new WCHAR[4].

In this case, it's actually the result of assignment to myWchar used as the initialization for p, but even if the structure was:

PWSTR myWchar = NULL;
WCHAR *p;
p = myWchar = new WCHAR[4];

so it was all assignment, no initialization, assignment is right-to-left associative, so it would occur in the same order (it just would use assignment rather than initialization semantics for the assignment to p, which could matter for custom types).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Knowing this site, this will be closed and deleted as it's a duplicate. (All C++ questions are a duplicate of "can you give me the C++ specification.") But it's a good answer. Have an upvote, and enjoy it whilst you can. – Bathsheba Aug 16 '23 at 16:30