Struct equivilence

This is a discussion on Struct equivilence within the C++ Programming forums, part of the General Programming Boards category; what would be the most likely equivilence for a struct? C programmers LOVE structs, but as a noob C++ programmer, ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    Struct equivilence

    what would be the most likely equivilence for a struct? C programmers LOVE structs, but as a noob C++ programmer, I'm not well versed with them.

    EX:
    Code:
    typedef struct{
     float x, y;
     float z;
     float r, g, b;
    } Vert;
    
    typedef struct{
     int n;
     Vert *verts;
    }Poly;
    Would a class replace a struct? If a class wasn't necessarily the best way to go, is there another way to do this without using struct?

    thanks for not flaming me too bad

    tms

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Well ... yeah. Why not.

    But if you want to look into classes you'll need to understand scructs really. Just cause C++ uses classes it doesn't mean you don't use structs. If you want something to simply hold data use a struct, but if you need to use a lot of functions along with the data try a class.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There are structs in C++. They are the same as classes, except they default to public access instead of private. Unlike in C, they do not require the typedef to be used as types, so the equivalent in C++ could be:
    Code:
    struct Vert {
     float x, y;
     float z;
     float r, g, b;
    };
    
    struct Poly {
     int n;
     Vert *verts;
    };
    or:
    Code:
    class Vert {
    public:
     float x, y;
     float z;
     float r, g, b;
    };
    
    class Poly {
    public:
     int n;
     Vert *verts;
    };

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    ok, then how do I access the structs? Is it customary to put the structs in a different file or above main?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    For large projects, the structure or class definitions would certainly be in separate files. For a small project, you could put them in the same file if you wanted to.

    If you need to access those structures in multiple source files, then the structure needs to be in a header file so that you can include it in multiple source files.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    tyvm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Replies: 1
    Last Post: 12-03-2008, 02:10 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. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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