Thread: Class: Multidimensional array initializer

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    9

    Class: Multidimensional array initializer

    So I have made my first class.
    It calculates the area of a triangle from the position of the vertices.

    I was wondering if it is possible to have only one variable, and not three (ver1, ver2, ver3), in the constructor?
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Triangle{
    public:
        float ver1[1][2];
        float ver2[1][2];
        float ver3[1][2];
    
    private:
        float area_formula(){
            float x1 = ver1[0][0];
            float y1 = ver1[0][1];
            float x2 = ver2[0][0];
            float y2 = ver2[0][1];
            float x3 = ver3[0][0];
            float y3 = ver3[0][1];
            return .5*(x2*y3-x3*y2-x1*y3+x3*y1+x1*y2-x2*y1);
        }
    
    public:
        float area(){
        return area_formula();
        } 
    };
    
    int main(){
        //float ver0[3][2] = { {0,0}, {1,0}, {1,1}};
        //Triangle t1 = ver0;    
        Triangle t1 = {{0,0}, {1,0}, {1,1}};
        cout << t1.area() <<endl;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well an array with dimension 1 is pretty pointless.

    Also, when you start adding 1,2,3 suffixes to your variable names, it's screaming to be an array.

    So don't you really have this?
    float verts[3][2];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I would also recommend just writing a vertex class.
    Code:
    template <
      typename T, // only enable our class for 'float' or 'double' types
      typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
    struct vertex
    {
      T x;
      T y;
    };
    And then do as Salem said,
    Code:
    template <typename T>
    class triangle
    {
    private:
      std::array<vertex<T>, 3> vertices;
    };
    You can definitely drop the templated parts and just manually hard-code `float` or `double` but just as an fyi, you can actually control template types with enable_if. It's a pretty neat feature!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    template
    <
        typename T, // only enable our class for 'float' or 'double' types
        typename = std::enable_if_t<std::is_floating_point_v<T>> // C++14
    >
    struct vertex
    {
      T x;
      T y;
    };
    (Didn't try to compile, though.)
    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.

  5. #5
    Guest
    Guest
    The _v types aren't implemented in any stable compiler releases yet afaik.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Yeah, I normally just manually polyfill a lot of that functionality and will just type:
    Code:
    template <bool B, typename T = void>
    using enable_if_t = typename std::enable_if<B, T>::type;
    
    template <typename T>
    constexpr bool is_floating_point_v = std::is_floating_point<T>::value;
    Note, these are ripped pretty much straight from cppreference.com

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm continually amused by those who pollute and complicate their implementation by supporting double and float, when float is such a garbage type.

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by whiteflags View Post
    I'm continually amused by those who pollute and complicate their implementation by supporting double and float, when float is such a garbage type.
    Well, I think `is_floating_point` also supports the `long double` type as well.

    `float` and `double` support make more sense in a CUDA context where size difference and hardware differences can have huge impacts on runtime performance. But supporting `float` still has uses in a CPU-based context as well. A `double` is twice as big as a `float` is and this can matter, for example, if you're allocating arrays.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Maybe I should have said, pick one or the other based on your specific usage restrictions, then.

  10. #10
    Registered User
    Join Date
    Aug 2016
    Posts
    9
    Wow, that was many replies!
    I must have forgotten to ask for getting e-mail when there are replies in this thread, since I haven't gotten any mail and there's been a lot of activity!
    Shall start looking through your answers!

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Yeah, we're just trying to go all Enterprise FizzBuzz on your simple code snippet XD

    @whiteflags

    I never actually asked why you think float is a garbage type. So, why do you think it's a garbage type? I mean, it's not float's fault that it's inaccurate. Blame binary representations of decimals and users trying to always freaking divide 1 by 10.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think float is a garbage type because it is the short int (or maybe even the char) of the floating point world.
    Last edited by whiteflags; 08-20-2016 at 02:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extended initializer lists flag on class constructor
    By killme in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2013, 02:44 AM
  2. Help with Static initializer in array
    By spanlength in forum C Programming
    Replies: 3
    Last Post: 07-06-2012, 10:46 AM
  3. 3D array initializer list
    By kenryuakuma in forum C# Programming
    Replies: 5
    Last Post: 11-15-2009, 01:40 AM
  4. Initializing multidimensional array in a class
    By shiju in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2007, 01:06 PM
  5. Array problem - inavlid initializer
    By Shadow in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2002, 01:40 PM

Tags for this Thread