more that one struct

This is a discussion on more that one struct within the C++ Programming forums, part of the General Programming Boards category; in this header i have declared a struct can i declare another one in the same header (i kno i ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    57

    more that one struct

    in this header i have declared a struct can i declare another one in the same header (i kno i probally can....)
    but do i just declare another one right after or.......

    here is the code

    #ifndef _Dagger_H
    #define _Dagger_H
    struct weapon{
    char type[30];
    char name[30];
    int tohitmod;
    int damage;
    int damagemod;
    };
    // another struct here??
    #endif
    =@-OmegatronO-@=

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can declare as many structs as you want or need as long as the names don't clash.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    57
    Thanks again Prelude
    =@-OmegatronO-@=

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Anytime

    -Prelude
    My best code is written with the delete key.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I usually use typedef when creating structures, then I can easily create new ones based on the "original":

    Code:
    typedef struct
    {
      int Var1;
      int Var2;
      char* String;
    }MYSTRUCT;
    
    MYSTRUCT MyStruct;
    MYSTRUCT AnotherStruct;
    MYSTRUCT AThirdStruct;
    
    MyStruct.Var1=20;
    AThirdStruct.Var2=15;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Unregistered
    Guest

    a question about structs then....

    with a class I have learnt to do this...

    class Myclass
    {
    ....
    ....
    ....
    };

    Myclass classExample1;
    Myclass myclassArray [10];

    can the saem be doe with structs or must I use typedef to accomplish this, like in the last post?

  7. #7
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>can the saem be doe with structs

    Yes.

    Structs and classes are almost identical.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    The only difference between structs and classes in C++ are that structs' "default level" of data access is public as opposed to classes' private.

  9. #9
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    But we tend to use structs for different uses than classes. How often do you see someone put a function in a struct?

  10. #10
    Unregistered
    Guest
    Greetings,

    In C++, structs "are" classes in C++, and don't need the "typedef". Try this code for example:

    Code:
    #include <iostream>
    
    #define USE_STRUCT // remove this to use the "class" version
    
    #ifdef USE_STRUCT
    struct MyClass {
        MyClass() {}
        virtual ~MyClass() {}
        void set(int newVal) { some_var = newVal; }
        int get() { return some_var; }
    private:
         int some_var;
    };
    #else
    class MyClass {
        int some_var;
    public:
        MyClass() {}
        virtual ~MyClass() {}
        void set(int newVal) { some_var = newVal; }
        int get() { return some_var; }
    };
    #endif
    
    int main() 
    {
        MyClass myclass;
        myclass.set(10);
        std::cout << myclass.get() << std::endl;
        return 0;
    }
    Da only diference is, like rmullen3 said, that members default to public in structs, and to private in classes! Apart from that the 2 keywords mean the same thing!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21