slint/api/cpp/tests/multiple-includes/main.cpp
Olivier Goffart 2f2116686f C++: forward declare explicitly to make the code more robust
We currently forward declare classes as we use them in functions.
But this breaks if classes with the same name were declared in the
parent namespace. As shown with this example

```C++
// Uncomment that line to make the code break
//struct SharedGlobals;

namespace ns {
    // Is that a forward declaration in `ns`?
    // Depends if it was declared before in the parent namespace
    void foo(struct SharedGlobals *x) {}

    // Actualy define ns::SharedGlobal
    struct SharedGlobals { int x; };

    int xyz() {
        SharedGlobals globals;
        foo(&globals);
    }
}
```

So make sure we forward-declare the classes properly to be more robust
and be able to include generated file with namespace after a file
without namespace

CC #2909
2025-05-26 18:32:09 +02:00

18 lines
508 B
C++

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
#include "logic.h"
#include "app-window.h"
#include "another-window.h"
int main(int argc, char **argv)
{
auto my_ui = App::create();
setup_logic(my_ui->global<Logic>());
auto my_ui2 = other::AnotherWindow::create();
my_ui2->global<other::Logic>().on_decrement([](int x) { return x - 1; });
my_ui2->show();
my_ui->run();
}