Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Storing different types

45 views
Skip to first unread message

Heinz-Mario Frühbeis

unread,
May 28, 2016, 6:24:04 AM5/28/16
to
Hi,

it took many time over the last times, because it is a great wish of
mine to have this... different types in one collection (or such).

Now I have a solution..., first is an array, second is with a vector.

The array is working well, output is:
1000
700
300
NOT FOUND
NOT FOUND
FOUND at 2
NOT FOUND
NOT FOUND

But the vector "makes" problems..., the output is:
PUSHED_BACK 0x7fff63053760 1
PUSHED_BACK 0x7fff63053770 1
PUSHED_BACK 0x7fff63053780 2
Das Programm ist abgestürzt. // The program has crashed

The (first) question is: Why hasn't the vector 3 elements?

Below you will find the source...

A first other question is:
Is there another, better way to realize it? (no void*, no boost...)

Regards
Heinz-Mario Frühbeis


#include <iostream>
#include <vector>

using namespace std;

void To_Array();
void To_Vec();

class A{
public:
int val;
};

class B{
public:
int val;
};

template<typename t>
t* Add(int index, t *vVal = NULL, int vSet = 0){
static t* Values[100];
if(vSet == 1){
Values[index] = vVal;
}
return Values[index];
}

template<typename t>
t* Add_Vec(int index, t *vVal = NULL, int vSet = 0){
static std::vector <t*> vec;
if(vSet == 1){
vec.push_back(vVal);
cout << "PUSHED_BACK " << vVal << " " << vec.size() << endl; cout.flush();
}
return vec[index];
}

int main(){
To_Array(); // This is working
//To_Vec(); // Crash
return 0;
}

void To_Vec(){
A c_A;
B c_B;
A c_C;

c_A.val = 300;
c_B.val = 700;
c_C.val = 1000;

Add_Vec <A> (0, &c_A, 1);
Add_Vec <B> (1, &c_B, 1);
Add_Vec <A> (2, &c_C, 1);

cout << Add_Vec <A> (2)->val << endl; cout.flush();
cout << Add_Vec <B> (1)->val << endl; cout.flush();
cout << Add_Vec <A> (0)->val << endl; cout.flush();

for(int i = 0; i < 3; i++){
if( Add_Vec <A> (i) == &c_C){
cout << "FOUND at " << i << endl; cout.flush();
} else {
cout << "NOT FOUND \n"; cout.flush();
}
}
}

void To_Array(){
A c_A;
B c_B;
A c_C;

c_A.val = 300;
c_B.val = 700;
c_C.val = 1000;

Add <A> (0, &c_A, 1);
Add <B> (1, &c_B, 1);
Add <A> (2, &c_C, 1);

cout << Add <A> (2)->val << endl; cout.flush();
cout << Add <B> (1)->val << endl; cout.flush();
cout << Add <A> (0)->val << endl; cout.flush();

for(int i = 0; i < 5; i++){
if( Add <A> (i) == &c_C){
cout << "FOUND at " << i << endl; cout.flush();
} else {
cout << "NOT FOUND \n"; cout.flush();
}
}
}

Öö Tiib

unread,
May 28, 2016, 7:38:11 AM5/28/16
to
On Saturday, 28 May 2016 13:24:04 UTC+3, Heinz-Mario Frühbeis wrote:
> it took many time over the last times, because it is a great wish of
> mine to have this... different types in one collection (or such).

Alternative types put into one type is called 'union' in C and C++.
Search it up, it must be in C++ textbook you use for learning it.
The union objects can be elements of containers and arrays.

Can you tell ... why you have such a great wish? What you need it for?

>
> Now I have a solution..., first is an array, second is with a vector.
>
> The array is working well, output is:
> 1000
> 700
> 300
> NOT FOUND
> NOT FOUND
> FOUND at 2
> NOT FOUND
> NOT FOUND
>
> But the vector "makes" problems..., the output is:
> PUSHED_BACK 0x7fff63053760 1
> PUSHED_BACK 0x7fff63053770 1
> PUSHED_BACK 0x7fff63053780 2
> Das Programm ist abgestürzt. // The program has crashed

The 'Add_Vec <A> (2)->val' does likely crash at 'return vec[index];' that
has undefined behavior. If you want defined behavior (but not much more
pleasant) then perhaps use 'return vec.at(index);' ... that will throw
instead of maybe crashing.

>
> The (first) question is: Why hasn't the vector 3 elements?

What "the vector" you mean? The static vector in 'Add_Vec<A>' will have
2 elements and the static vector in 'Add_Vec<B>' will have one element
if your code actually calls 'To_Vec' (call is commented out in posted
code but seems to be happening in posted output).

>
> Below you will find the source...
>
> A first other question is:
> Is there another, better way to realize it? (no void*, no boost...)

To realize what? There is 'union'. If you want examples of reinventing or
extending it well then there is 'boost::variant'. There are lot of other
mature "Variant" class implementations in various frameworks like COM,
Qt etc. Also there are (less convenient) "Any" class implementations.
Search engine vomits additionally lot of square wheels invented to that
theme.

What you particularly attempt to do does not work. You seemingly assume
that different instantiations of a template function somehow share
internal static variables. These do not.

Jorgen Grahn

unread,
May 31, 2016, 2:39:19 AM5/31/16
to
On Sat, 2016-05-28, Öö Tiib wrote:
> On Saturday, 28 May 2016 13:24:04 UTC+3, Heinz-Mario Frühbeis wrote:
>> it took many time over the last times, because it is a great wish of
>> mine to have this... different types in one collection (or such).
>
> Alternative types put into one type is called 'union' in C and C++.
> Search it up, it must be in C++ textbook you use for learning it.
> The union objects can be elements of containers and arrays.
>
> Can you tell ... why you have such a great wish? What you need it for?

My guess (and it's only a guess) is that:

- He's used to Python.
- The right solution is on a higher level: to learn to design in C++
without that feature.

The good news is that I've used both, and don't feel limited by the
lack of the feature in C++. Mixed containers fit nicely with other
Python features, but are kind of alien in C++.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages