Thread: Class or Structure or Union?

  1. #1
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130

    Class or Structure or Union?

    If I needed to make a type for a special purpose than I could make a structure or a union, however C++ allows you to create a class. If I only wanted data inside of the type though (no methods), would it still make more sense to make a class instead of a structure? and say I wanted to store this type in a vector because the vector supports more operations than an array or an array of pointers to dynamically allocated types. Is a class type the way to go or not?

    Now as far as I know I can access the members of the data type in either an array or a vector but an array has an index whereas in a vector I would use a variable of the same data type and iterate through the vector with something like a for instance loop, until I found the matching data type member.

    Which would I use? What would I do. Or should I pack it up!

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    A class is a struct. The only literal difference is default visibility. If you choose a class or a struct with private visibility (logically equivalent to a class), you'll need to implement either a constructor or a series of getters and setters. So no, it wouldn't make more sense to use private visibility over public.

    Also, I'm not sure what you mean by, "Now as far as I know I can access the members of the data type in either an array or a vector..." because when you write,
    Code:
    struct data_store
    {
        int x;
    };
    , you access the members via
    Code:
    data_store ds;
    ds.x = 4;
    so there's no array or vector there.

  3. #3
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    The main thing is that I always used a structure, but I was programming in C and just a little bit of C++. I will use a class instead of a structure for my next program.

    If you can put variables of class type into a vector than that is better than using an array, although is there cases where an array is better than a vector?

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Um... An vector is an array. Well, mostly.

    I think you're trying to allude to a structure of arrays vs an array of structures approach. Is that what you mean?

    Consider :
    Code:
    // structure of arrays
    struct soa
    {
        std::vector< int > x, y, z;
    };
    vs.

    Code:
    struct point
    {
        int x, y, z;
    };
    
    std::vector< point > aos;
    Is this what you mean?

    Edit : Your English just honestly isn't very clear. Start posting more code, please.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    Um... An vector is an array. Well, mostly.
    No.
    An array is a set of elements with a static size. Hence it can be allocated on the stack.
    A vector is a set of elements with a dynamic size. Hence, it has the overhead of allocating and managing heap memory, as it cannot be allocated on the stack.

    Aside from that, I want to keep them separate as their interfaces differ. True, C arrays don't offer anything at all regarding interface, but std::array (C++ arrays) do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Elysia View Post
    No.
    An array is a set of elements with a static size. Hence it can be allocated on the stack.
    A vector is a set of elements with a dynamic size. Hence, it has the overhead of allocating and managing heap memory, as it cannot be allocated on the stack.

    Aside from that, I want to keep them separate as their interfaces differ. True, C arrays don't offer anything at all regarding interface, but std::array (C++ arrays) do.
    Well, obviously. But that's an implementation detail.

    The point of both though is that they're contiguous storage of the same type T and you access elements in a very similar fashion (a dereference of a pointer offset). To me, they're logically equivalent because they roughly fill the same function.

    Actually, I'm totally guilty of doing this when I want to simulate dynamic arrays using a stack-based array :
    Code:
    const size_t n = 16;
    
    T data[ n ];
    size_t back = 0;
    
    /* emulate std::vector::push_back() */
    
    data[ back++ ] = /* ... */
    You could actually design a proper class for this which I'm too lazy to do atm but you get the idea. I guess the stack vs heap nature would be an important distinction for a beginner though.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    Well, obviously. But that's an implementation detail.
    That's not an implementation detail. That's the same as saying a list is the same as an array. You could implement an array with both data structures, but that has a big affect on the code.
    An array and a vector represents two different ways of implementing a contiguous set.
    An implementation detail could be that you're using two pointers (begin, end) to keep track of the data, or a pointer and a size.

    The point of both though is that they're contiguous storage of the same type T and you access elements in a very similar fashion (a dereference of a pointer offset). To me, they're logically equivalent because they roughly fill the same function.
    They don't fill the same function. You're just thinking of vector as a dumb array. It's not. It provides a lot more functionality than a dumb C array. That "something" is part of what defines its function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Elysia View Post
    An array is a set of elements with a static size. Hence it can be allocated on the stack.
    You can dynamically allocate arrays on the stack, actually, via alloca().

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The important difference between vector and everything else is that vectors will grow naturally, they can be resized, they can even be empty. That is not necessarily true of arrays, and it's what separates those things apart.

    I gave Elysia a like before because I thought that's what he's saying, but now he's saying a bit more than I would about arrays and vectors. It's an important, simple difference that deserves to be said outright.

    An array and a vector represents two different ways of implementing a contiguous set.
    Well, ideally you wouldn't pick vector because it does things differently, but because the size of the array is huge, or variable, or unknown at compile time.

  10. #10
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    That's not an implementation detail.
    An array and a vector represents two different ways of implementing a contiguous set.
    Ha!

    You can emulate a lot of the behavior of a vector in an array though!
    Code:
    // g++ -std=c++11 -Wall -Wextra -O3 -o vecarray vecarray.cpp
    // Output :
    // 0
    // 1
    // 2
    // 3
    
    
    #include <iostream>
    
    
    template< typename T, size_t n >
    struct vecarray
    {
    
    
    private :
    
    
        T _data[ n ];
        size_t _back;
    
    
    public :
    
    
        vecarray( void )
        {
            _back = 0;
        }
    
    
        void push_back( const T &tmp )
        {
            _data[ _back++ ] = tmp;
        }
    
    
        size_t size( void )
        {
            return _back;
        }
    
    
        T operator[]( const size_t idx )
        {
            return _data[ idx ];
        }
    };
    
    
    int main( void )
    {
        vecarray< int, 16 > va;
    
    
        for ( int i = 0; i < 4; ++i )
        {
            va.push_back( i );
        }
    
    
        for ( size_t i = 0; i < va.size(); ++i )
        {
            std::cout << va[ i ] << std::endl;
        }
    
    
        return 0;
    }
    Granted, you can't do a resize but there's a lot of overlap between the two. Yes, they are not exactly the same but I mean, close enough?
    Last edited by MutantJohn; 08-28-2015 at 02:58 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Yarin View Post
    You can dynamically allocate arrays on the stack, actually, via alloca().
    Yeah I know arrays can be allocated dynamically on the stack. The C standard has supported since long ago. It's not yet in the C++ standard, though. No idea if it ever will be.

    Quote Originally Posted by whiteflags View Post
    Well, ideally you wouldn't pick vector because it does things differently, but because the size of the array is huge, or variable, or unknown at compile time.
    This is a good reason why a vector is not just an implementation detail. A vector is different enough from a dumb array that it's something a programmer should explicitly choose. Another tool in the toolbox, so to speak.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Granted, you can't do a resize but there's a lot of overlap between the two. Yes, they are not exactly the same but I mean, close enough?
    Close enough, but it's not a personal crusade at all. I just don't appreciate discussions on abstract data structures, while simultaneously descussing concrete implementations like std::array, C arrays, and std::vector, which all offer different things. The discussion can become confusing, and C++ duplicating behaviors just to offer other features in places does not help.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    Ha!

    You can emulate a lot of the behavior of a vector in an array though!

    Granted, you can't do a resize but there's a lot of overlap between the two. Yes, they are not exactly the same but I mean, close enough?
    Now you've just implemented a basic version of std::array, which again, is separate from std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    *flips desk over*

    Edit : This is supposed to be a joke.
    Last edited by MutantJohn; 08-28-2015 at 03:20 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I've destroyed lamps and monitors doing that - be careful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. something not a structure or union
    By programmerc in forum C Programming
    Replies: 2
    Last Post: 08-23-2014, 10:30 AM
  2. Using Structure in union problem
    By aarathy in forum C Programming
    Replies: 9
    Last Post: 05-27-2013, 11:54 PM
  3. Structure Vs Union
    By mgnidhi_3july in forum C Programming
    Replies: 17
    Last Post: 06-16-2010, 09:45 PM
  4. structure and union
    By BEN10 in forum C Programming
    Replies: 10
    Last Post: 06-24-2009, 11:30 PM
  5. Structure / Union Error
    By JimpsEd in forum C Programming
    Replies: 2
    Last Post: 11-11-2006, 01:03 PM