-1
/* ..CODE.. */
struct st_Settings {
    struct {
        unsigned int x;
        unsigned int y;
        unsigned int width;
        unsigned int height;
    } window;
} defaultSettings;
/* ..CODE.. */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) {
    using st_Settings; // default settings
    {
        using window;
        {
            x = 50;
            y = 50;
            width = 800;
            height = 600;
        }
    }
    /* ..CODE.. */
}

This code doesnt work, and this is my question, can i replace "using" keyword with something else what working with structures?


Okey i have struct like this:

struct
{
    struct a
    {
        int a;
        int b;
        int c;
    }
    struct b
    {
        struct a
        {
            int a;
            int b;
            int c;
        }
        struct b
        {
            int a;
            int b;
            int c;
        }
    }
    struct c
    {
        int a;
        int b;
        int c;
    }
} a;

Did i must do this:

a.a.a = 1;
a.a.b = 12;
a.a.c = 14;

a.b.a.a = 41;
a.b.a.b = 61;
a.b.a.c = 34;

a.b.b.a = 65;
a.b.b.b = 45;
a.b.b.c = 23;

a.c.a = 1;
a.c.b = 0;
a.c.c = 4;

Or it is possible to do something like this:

a.
{
    a.
    {
        a = 1;
        b = 12;
        c = 14;
    }

    b.
    {
        a.
        {
            a = 41;
            b = 61;
            c = 34;
        }
        b.
        {
            a = 65;
            b = 45;
            c = 23;
        }
    }

    c.
    {
        a = 1;
        b = 0;
        c = 4;
    }
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • 3
    That's not how `using` is used in C++. Get yourself one or more of [these books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jan 27 '14 at 14:37
  • The using keyword is applicable for namespace. – Mihai8 Jan 27 '14 at 14:37
  • You are mis*using* and ab*using* `using`, quite conf*using* – Paul Evans Jan 27 '14 at 14:39
  • @user1929959: actually, I'm not even sure what you're trying to say. Oo – thokra Jan 27 '14 at 14:40
  • Using does not do that in c++, it has an entirely different function. There isn't any keyword in c++ that does what you want. You *might* be able to create a variable containing a reference to the sub-object you want to access fields in that to simplify your code, but really, just write the whole thing out, it's clearer, just more typing – jcoder Jan 27 '14 at 14:59
  • You're looking for a keyword like Pascal's `with`, which [doesn't exist in C++](http://stackoverflow.com/q/2279180/1782465). – Angew is no longer proud of SO Jan 27 '14 at 15:10
  • Yes, I searching for something like Pascal's "with" keyword – user3033514 Jan 27 '14 at 15:15

2 Answers2

0

The using keyword is not being used properly in your code (and is not needed at all in this segment of code):

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) 
{
    st_Settings settings;
    settings.window.x = 50;
    settings.window.y = 50;
    settings.window.width = 800;
    settings.window.height = 600;
    // ...
}

Also note that the way you're defining your structure is defining a global instance named defaultSettings. If you wanted to use that instead, you would remove the line declaration of settings above, and replace all other instances of it with defaultSettings.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • Yes i can but this not looking clearly. What if my struct is larger? I looking for something like namepspace but can be used with structures. – user3033514 Jan 27 '14 at 14:45
  • @user3033514 "something like namespace but can be used with structures" - that makes no sense. A structure (or class) is a custom data type. A namespace is just a named grouping for data types. – Zac Howland Jan 27 '14 at 14:57
0

The only thing near to what you want is initialize the structure when declaring the variable:

struct st_Settings {
    struct {
        unsigned int x;
        unsigned int y;
        unsigned int width;
        unsigned int height;
    } window;
} defaultSettings = {{50,50,800,600}};

at the same time, you can initialize inside a function:

int a = 10 ;
int b = 20 ;
struct st_Settings declaredAndSetted = {{a,b,defaultSettings.window.width,400}} ;

If you use c++0x standard ($ g++ -std=c++0x <file>) you can reassign values to a structure variable in this way:

defaultSettings = {{a,b,defaultSettings.window.width,400}} ;
Alfonso Nishikawa
  • 1,876
  • 1
  • 17
  • 33