I have managed to get a few things to work.
Maybe some example code will help to focus the conversation.
(Sorry to paste my code in-line. I can't find any way to create a "text block" for code entry like I see used in other posts. And I can't find any instructions on how to use this forum's editing capabilities.)
Header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include "StdTypDefs.h" // U8, S8, U16...
template<typename T, size_t SIZE>
class DataTyp
{
private:
static T Data[SIZE];
static const size_t size = SIZE;
U16 num;
public:
DataTyp(U16 elemNum)
{
num = (elemNum < (size - 1)) ? elemNum : size;
};
U16 GetNum() { return num; };
void SetVal(U16 const& val) { Data[num] = val; };
U16 &GetVal() { return Data[num]; };
T& operator[] (U16 i)
{
return (i < size) ? Data[i] : num; // This use of "num" only for testing.
};
const T& operator[] (U16 i) const
{
return (i < size) ? Data[i] : num; // This use of "num" only for testing.
};
};
template<> U16 DataTyp<U16, 16>::Data[16];
template<> S16 DataTyp<S16, 16>::Data[16];
template<> U32 DataTyp<U32, 16>::Data[16];
|
CPP file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include "stdafx.h"
#include<iostream>
#include "StdTypDefs.h"
#include "RefArray_01.h"
using namespace std;
int main()
{
U16 A_U16 = 123;
U16 B_U16 = 456;
U16 C_U16 = 789;
DataTyp<U16, 16> val_U16_A(0);
DataTyp<U16, 16> val_U16_B(5);
DataTyp<U16, 16> val_U16_C(9);
val_U16_A.SetVal(A_U16);
val_U16_B.SetVal(B_U16);
val_U16_C.SetVal(C_U16);
cout << "val_U16[" << val_U16_A.GetNum() << "] = " << val_U16_A.GetVal() << endl;
cout << "val_U16[" << val_U16_B.GetNum() << "] = " << val_U16_B.GetVal() << endl;
cout << "val_U16[" << val_U16_C.GetNum() << "] = " << val_U16_C.GetVal() << endl << endl;
DataTyp<U16, 16> val_U16_tmp(0); // Second reference to same element as val_U16_A.
val_U16_tmp.SetVal(777);
cout << "val_U16[" << val_U16_A.GetNum() << "] = " << val_U16_A.GetVal() << endl;
cout << "val_U16[" << val_U16_B.GetNum() << "] = " << val_U16_B.GetVal() << endl;
cout << "val_U16[" << val_U16_C.GetNum() << "] = " << val_U16_C.GetVal() << endl << endl;
U16 tmp;
tmp = val_U16_B[5];
val_U16_A[0] = tmp;
cout << "val_U16[" << val_U16_A.GetNum() << "] = " << val_U16_A.GetVal() << endl << endl;
int i;
cin >> i;
return 0;
}
|
This runs on Visual Studio Express 2015 and produces this consul output:
val_U16[0] = 123
val_U16[5] = 456
val_U16[9] = 789
val_U16[0] = 777
val_U16[5] = 456
val_U16[9] = 789
val_U16[0] = 456 |
So I have something very ragged working that comes close to part of what I want.
This template instantiation format seems to work:
template<> U16 DataTyp<U16, 16>::Data[16];
But:
- It would be nice if I could avoid the redundant type and size values.
- Browsing the web, I found a wide variety of opinions on the proper format for this and the proper file (H or CPP). Some of it conflicting.
This value instantiation format is NOT so acceptable:
DataTyp<U16, 16> val_U16_A(0);
I want to be able to declare DataTyp instances using a constructor with only a type specifier. In this case, the class would have a static "cnt" member that's initialized to zero when the Data array is allocated and "size is set.
Then, when a class instance is declared, "num" member is set to cnt++ so each declared value is associated with the next element of Data[].
- I couldn't figure out how to get "static const cnt" initialized.
The operator[] members in this example allows external selection of random subscript numbers. This is NOT desired behavior.
my original concept war to create a value reference type composed of:
{typeNumber, instanceNumber }
That could act a a reference to an element in an array of a specific type.
Then I could make lists of that type and have them reference any value of any type (within the allocated arrays).
I found no solution that efficiently uses "typeNumber" to select the correct array and return a reference value for that type.
I was hoping that templates for this example DataTyp class would automatically use the correct DataTyp instantiation based on the function arguments of the member functions. Then I need only the instanceNumber.
So I think I'm headed down a dead-end road right now.
I'm in over my head and I need some help digging my way out.
P.S. Preview doesn't work for me (IE or Firefox) and the topic editor tells me that this post is 67 characters long.
P.P.S. I got an error when I tried to post. Maybe from experimenting with format buttons. I was able to cut-and -paste the content into a new post, and this one worked.