in Education by
I have a bunch of vector classes. I have a 2D point vec2_t, a 3D point vec3_t and a 4D point vec4_t (you often want these when you have do graphics; this is graphics code, but the question has a generic C++ flavour). As it is now, I have vec2_t declaring two members x and y; vec3_t subclasses vec2_t and has a third member z; vec4_t subclasses vec3_t and adds a w member. I have a lot of near-duplicate code for operator overloading computing things like distances, cross products, multiplication by a matrix and so on. I've had a few bugs where things have been sliced when I've missed to declare an operator explicitly for the subclass and so on. And the duplication bugs me. Additionally, I want to access these members as an array too; this would be useful for some OpenGL functions that have array parameters. I imagine that perhaps with a vec_t template I can make my vector classes without subclassing. However, this introduces two problems: How do you have a variable number of members that are also array entries, and ensure they align? I don't want to lose my named members; vec.x is far nicer than vec.d[0] or whatever imo and I'd like to keep it if possible How do you have lots of the more expensive methods in a CPP source file instead of the header file when you take the templating route? One approach is this: struct vec_t { float data[3]; float& x; float& y; float& z; vec_t(): x(data[0]), y(data[1]), z(data[2]) {} }; Here, it correctly aliases the array members with names, but the compiler I've tested with (GCC) doesn't seem to work out they are just aliases and so the class size is rather large (for something I might have an array of, and want to pass e.g. as a VBO; so size is a big deal) and how would you template-parameterise it so only the vec4_t had a w member?) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
A possible solution (I think). main.cpp: #include #include "extern.h" template struct vec_t_impl { int values[S]; bool operator>(const vec_t_impl& a_v) const { return array_greater_than(values, a_v.values, S); } void print() { print_array(values, S); } virtual ~vec_t_impl() {} }; struct vec_t2 : vec_t_impl<2> { vec_t2() : x(values[0]), y(values[1]) {} int& x; int& y; }; struct vec_t3 : vec_t_impl<3> { vec_t3() : x(values[0]), y(values[1]), z(values[2]) {} int& x; int& y; int& z; }; int main(int a_argc, char** a_argv) { vec_t3 a; a.x = 5; a.y = 7; a.z = 20; vec_t3 b; b.x = 5; b.y = 7; b.z = 15; a.print(); b.print(); cout << (a > b) << "\n"; return 0; } extern.h: extern bool array_greater_than(const int* a1, const int* a2, const size_t size); extern void print_array(const int* a1, const size_t size); extern.cpp: #include <iostream> bool array_greater_than(const int* a1, const int* a2, const size_t size) { for (size_t i = 0; i < size; i++) { if (*(a1 + i) > *(a2 + i)) { return true; } } return false; } void print_array(const int* a1, const size_t size) { for (size_t i = 0; i < size; i++) { if (i > 0) cout << ", "; std::cout << *(a1 + i); } std::cout << '\n'; } EDIT: In an attempt to address the size issue you could change the member reference variables to member functions that return a reference. struct vec_t2 : vec_t_impl<2> { int& x() { return values[0]; } int& y() { return values[1]; } }; Downside to this is slightly odd code: vec_t2 a; a.x() = 5; a.y() = 7;

Related questions

0 votes
    I have a bunch of vector classes. I have a 2D point vec2_t, a 3D point vec3_t and a 4D point ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    ______ splits a data frame and results an array (hence the da). Hopefully you're getting the idea here. ... Regression of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    Why does a sorted array get processed faster than an unsorted array, even though the size of the arrays are ... processed faster. Why? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Consider the following code. What is a good hashing function for the array in Key to be used in ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I am trying to implement the Josephus election problem using an array as a mock circular linkedlist. The item and next arrays represent ... array x = next[x]; count++; } std::cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Which of this access specifies can be used for a class so that its members can be accessed by a different class ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these access specifiers can be used for a class so that its members can be accessed by a different ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    What is the process by which we can control what parts of a program can access the members of a ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    Write a function to extract the first name in the string Mrs. Jake Luther ? (a) Substring (b) Substr ( ... Debugging of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a struct A that is defined as follows: typedef struct A { CvRect B; // rect int C; ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
...