initializing static struct 
Author Message
 initializing static struct

Quote:

>struct MyClass::MyStruct MyStructInstance = new struct MyClass::MyStruct;

AFAIK, you would only do this with a MyStruct*

Have you tried
MyClass::MyStruct = { field1, field2};  ??

Quote:
>void main (void)
>{
>MyClass::MyStructInstance++;

Unless you have overloaded operator++() I don't know what you think
should happen here?

Mike

--

"Genius gives birth, talent delivers."

           -- Jack Kerouac

(Remove NOSPAM, if present, to reply via email)



Sat, 29 Jun 2002 03:00:00 GMT  
 initializing static struct
How do I initialize a static var that is a struct?

This is basically what I'm doing:

class MyClass
{
    public:
        struct MyStruct
            {        int var1;
                     int var 2;    };
        static MyStruct MyStructInstance;

Quote:
}

/* this is where I'm having trouble - I've tried many different things
here - this is just one example
 I've tried initializing each member of the struct too - but of course that
didn't work either */
struct MyClass::MyStruct MyStructInstance = new struct MyClass::MyStruct;

void main (void)
{
MyClass::MyStructInstance++;

Quote:
}



Sun, 30 Jun 2002 03:00:00 GMT  
 initializing static struct
Thanks.

I'm just a humble hobbyist programmer - no formal training - and my
tutorials and books are all c++ - so don't focus much on structs.


Quote:

> >struct MyClass::MyStruct MyStructInstance = new struct MyClass::MyStruct;

> AFAIK, you would only do this with a MyStruct*

> Have you tried
> MyClass::MyStruct = { field1, field2};  ??

I have now - thanks.

Quote:

> >void main (void)
> >{
> >MyClass::MyStructInstance++;

> Unless you have overloaded operator++() I don't know what you think
> should happen here?

Um ... I was concentrating more on the initializing than on using the
struct - It should have been something like

MyClass:MyStructInstance.var1++;

Thanks again.



Sun, 30 Jun 2002 03:00:00 GMT  
 initializing static struct
Here is how,

In the header file do
class MyClass
{
    public:
        struct MyStruct
  {      
   int var1;
            int var2;
  };
        static MyStruct MyStructInstance;

Quote:
};

In the .cpp file do
// The following is necessary to provide a global storage location for the MyStruct data.
MyClass::MyStruct MyClass::MyStructInstance;

int main(int argc, char* argv[])
{
 MyClass::MyStructInstance.var1=10;

 return 0;

Quote:
}

Vincent Minden
Quote:

> How do I initialize a static var that is a struct?

> This is basically what I'm doing:

> class MyClass
> {
>     public:
>         struct MyStruct
>             {        int var1;
>                      int var 2;    };
>         static MyStruct MyStructInstance;
> }

> /* this is where I'm having trouble - I've tried many different things
> here - this is just one example
>  I've tried initializing each member of the struct too - but of course that
> didn't work either */
> struct MyClass::MyStruct MyStructInstance = new struct MyClass::MyStruct;

> void main (void)
> {
> MyClass::MyStructInstance++;
> }



Sun, 30 Jun 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Initialize struct with struct

2. vc wouldn't initialize static members in static libraries

3. struct initialization (with static, const static members)

4. Initialize array from members of a struct: Legal ?

5. Initializing const auto structs

6. padding in a statically initialized struct

7. Initializing an array of structs

8. how to initialize a struct

9. Initializing an Array of struct

10. Syntax for initializing structs

11. initializing pointer in struct with address of auto identifier

12. Initializing a pointer inside a struct

 

 
Powered by phpBB® Forum Software