1

Any idea why ** is used in this case?

HRESULT CreateSolidColorBrush(
  const D2D1_COLOR_F & color,
  ID2D1SolidColorBrush** solidColorBrush
);

The above is from Microsoft documentation https://learn.microsoft.com/en-us/windows/win32/api/d2d1/nf-d2d1-id2d1rendertarget-createsolidcolorbrush(constd2d1_color_f__id2d1solidcolorbrush)

What is the benefit of using a pointer to a pointer in this case?

(Why didn't they develop the function that just returned ID2D1SolidColorBrush*)

2
  • 2
    "Why didn't they develop the function that just returned ID2D1SolidColorBrush"* - because returning HRESULT is one of the COM fundamentals. Apr 5, 2020 at 14:52
  • It creates a new interface pointer, requires passing the pointer variable by reference. Ideally it would look like ID2D1SolidColorBrush* CreateSolidColorBrush( const D2D1_COLOR_F & color) but it needs to return adequate error info. Apr 5, 2020 at 14:55

1 Answer 1

2

1. What is the benefit of using a pointer to a pointer in this case?

Because that function is probably intented to modify a pointer you may pass giving its address.

When this method returns, contains the address of a pointer to the new brush. This parameter is passed uninitialized.

It may have been declared as reference to pointer, but it is more clear to user that he must give the address of the pointer to be modified, as it will enforce him the semantic.

2. Why didn't they develop the function that just returned ID2D1SolidColorBrush*?

Because the function already returns something that is a status.

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Separating status and computed value is a good practice.

5
  • I don't understand your answer, a pointer can be modified, why a pointer to a pointer? - second part when I said returned I meant via a pointer (rather that a pointer to a pointer) Apr 5, 2020 at 15:04
  • If you pass a pointer, you pass its value, not the pointer itself, thus if you want a function to modify one of its argument, the argument must be passed either by address or by reference. Apr 5, 2020 at 15:06
  • I get pass by reference. Example: the standard strcpy(char* destination, const char* source). It does not need char** destination to copy the string. What's the need for the extra pointer? Apr 5, 2020 at 15:58
  • 1
    the pointers aren't changed by strcpy, it only changes the chars themselves. by contrast, CreateSolidColorBrush isn't changing a brush object that is already there, its changing the pointer itself to point to the brush it creates, therefore the pointer must be passed by reference (via a pointer to it) in order to change it
    – kmdreko
    Apr 5, 2020 at 16:20
  • Ah, now I get it. Thanks for your patience in explaining this to me @kmdreko. Apr 6, 2020 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.