CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2009
    Posts
    12

    Question defining structs in a class?

    So if anyone read my last post this is the continuation of my saga to make an RPG game for learning and fun

    Just came across something:

    I have made a class for stats, another for resists. In each class I have defined a struct that has a list of variables mirroring that of the class. The structs are made to be used in and outside their class, specifically to use in the constructors of their parent class but also to pass out all the class variables in a getAll() method, rather than calling each getxxx() method individually.

    However to use both structs in a different class I have to #include both parent classes into the third. Would it be more efficient to just make a file that declares structs and include that into whatever needs em, rather than including many classes just to access the structs? If so how would I do that, a .cpp file?

  2. #2
    Join Date
    May 2005
    Location
    San Antonio Tx
    Posts
    44

    Re: defining structs in a class?

    In each class I have defined a struct that has a list of variables mirroring that of the class
    well, that sounds like redundant code, what exactly are you trying to do with that?

    Would it be more efficient to just make a file that declares structs and include that into whatever needs em, rather than including many classes just to access the structs?
    that's what header files are for, to separate things like declarations of classes and structs from the implemented code, that way it can be reused if needed.

    do all your class and structs declarations in a .h file and all the implementation of the code in .cpp files

  3. #3
    Join Date
    Dec 2009
    Posts
    12

    Re: defining structs in a class?

    Yes I am using .h and .cpp files

    here is some code

    class resistsObj
    {

    private:

    int fireR; //R = resist (aka "save")
    int fireS; //S = soak
    int lightR;
    int lightS;
    int coldR;
    int coldS;
    int poisonR; //poison and disease
    int poisonS;
    int mindR;
    int mindS;
    int holdingR;
    int holdingS;
    int magicR;
    int magicS;
    .
    .
    .

    that is in the .h file, in the .cpp file

    static struct allResists
    { //structure to store all resist values for easy transport
    int fireR;
    int fireS;
    int lightR;
    int lightS;
    int coldR;
    int coldS;
    int poisonR;
    int poisonS;
    int mindR;
    int mindS;
    int holdingR;
    int holdingS;
    int magicR;
    int magicS;
    .
    .
    .

    it may seem redundant but I really don't want a parameter list in the constructor a mile long

    the constructor:

    resistsObj::resistsObj(allResists resists)
    {
    .
    . //code assigning each resist to comparable in the struct
    .
    }

    Of course to pass that struct into this class means that I have to #include resistsObj somewhere else to access that struct.

    It seems wasteful to include a whole class just to access a static struct. Can I create a class that holds only structs, no methods? How would I do that? Or can I do something equivalent to
    #include "resistObj.h::allResists"

    Or is just including the class fine?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: defining structs in a class?

    Quote Originally Posted by SirLogiC View Post
    Yes I am using .h and .cpp files
    First, use code tags when posting code so that it appears formatted correctly.

    Second,
    Code:
    static struct allResists
    Can you explain what "static" means in this context? What is the reason for the "static" here?

    Third, structs are copyable by just stating "=". You don't need to copy elements one by one.
    Code:
    struct foo
    {
       int x;
       int y;
       foo(int x_, int  y_) : x(x_), y(y_){}
    };
    
    int main()
    {
       foo f(10, 20);
       foo f2( 20, 30);
       f1 = f2;  // f1 will now have the values of 20 and 30 for x and y.
    }
    Last, pass things such as structs and classes by reference or const reference instead of by value:
    Code:
    void SomeFunc(someStruct& s)
    {
    }
    There is a big difference if you leave the "&" out of that parameter.

    As I mentioned to you in another thread, C++ does not lend itself to trying things and seeing what sticks. There is more or less, a formal way of learning the language which takes weeks, if not months of immersion.

    If you don't do things this way where you are not understanding and using the fundamentals of the language correctly, you will wind up with a mess of a program.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: defining structs in a class?

    Quote Originally Posted by SirLogiC View Post
    It seems wasteful to include a whole class just to access a static struct.
    What is a "static struct"?

    If the struct is supposed to be accessed from the outside world, why is it a member of a class? Shouldn't it be standalone?
    Code:
    // here is mydata.h
    #ifndef MYDATA_H
    #define MYDATA_H
    struct MyData
    {
      // members
    };
    #endif
    Code:
    // here is some .cpp file that uses the struct MyData
    #include "mydata.h"
    
    // your cpp code here
    I don't see anything "wasteful" here.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: defining structs in a class?

    it may seem redundant but I really don't want a parameter list in the constructor a mile long
    So... don't put this stuff in the constructor (it's not required to put them there). Make them all public members so you can set them individually.

  7. #7
    Join Date
    Dec 2009
    Posts
    12

    Re: defining structs in a class?

    Quote Originally Posted by Paul McKenzie View Post
    What is a "static struct"?

    If the struct is supposed to be accessed from the outside world, why is it a member of a class? Shouldn't it be standalone?
    Code:
    // here is mydata.h
    #ifndef MYDATA_H
    #define MYDATA_H
    struct MyData
    {
      // members
    };
    #endif
    above is what I was asking, and thanks, that helps a lot

    static struct was my way of saying I wanted the struct separate from the class, but didn't know how to go about it. Again what's quoted above is very helpful

    Also thanks to all replies. I don't really understand the negative attitude a lot of posts have. I have said I am learning and with this I am driven. I am happy for any help or advice, I know there is many of the "finer things" as well as "obvious to me" things I don't know.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured