M
Marcelo De Brito
Hi!
I have created a template in which there is a generic vector/array
that can hold any type defined by the programmer or built-in types.
When I define the vector/array using built-in types (int, double,
etc.), I must insert data of the respective type in it. But when I
define the array using a programmer defined type (e.g., a class), I
can access the class' members even though I did not insert any object
in it! See the lines of code below for a better explanation.
class B { //Programmer defined type.
public:
B() {};
B(int idd) : id(idd) {};
B(const B&) {};
virtual ~B() {};
void setid(int idd) {id = idd;}
void dummy() {cout << "B::dummy()" << " " << "id = " << id <<
endl;}
private:
int id;
};
template <class T, int size = 10>
class D1 {
public:
D1() : index(0) {};
D1(const D1&) {};
virtual ~D1() {};
T vt[size];
void push(T* i) {
vt[index] = *i;
index++;
}
void exvet() {
for(int i = 0; i<index; i++)
cout << "vt[" << i << "] = " << vt << endl;
}
void vetsize() {cout << size << endl;}
private:
int index;
};
int main()
{
D1<int, 5> od4; //First with int.
for(int i = 0; i<5; i++)
od4.push(&i); //<- Inserting ints in the array. Fine.
od4.exvet(); //<- Shows the ints int the array, Fine too.
D1<B, 10> od5; //Now with the programmer defined type.
for(int i = 0; i<10; i++)
od5.vt.dummy(); //<- !!!!!!!HERE!!!!!! It compiles and run!
}
In my mind, when I declared "D1<B, 10> od5", I allocated memory for 10
elements of the type "B" -- as I did when I defined for the int type
"D1<int, 5> od4". BUT, in the "int" case, I had to insert data in the
vector "vt" of the template in order to properly access it. Therefore,
how could I access the members of "od5" if I did not insert any class
"B" object in the respective array "vt"?
Wouldn't a more suited way for this case be:
D1<B, 10> od5;
for(int i = 0; i<10; i++)
od5.push(new B); //Inserting class B objects.
for(int i = 0; i<10; i++)
od5.vt.dummy(); //Accessing members of the objects inserted.
How about it??
I appreciate any comment, suggestion, etc.
Thank You!
Marcelo
I have created a template in which there is a generic vector/array
that can hold any type defined by the programmer or built-in types.
When I define the vector/array using built-in types (int, double,
etc.), I must insert data of the respective type in it. But when I
define the array using a programmer defined type (e.g., a class), I
can access the class' members even though I did not insert any object
in it! See the lines of code below for a better explanation.
class B { //Programmer defined type.
public:
B() {};
B(int idd) : id(idd) {};
B(const B&) {};
virtual ~B() {};
void setid(int idd) {id = idd;}
void dummy() {cout << "B::dummy()" << " " << "id = " << id <<
endl;}
private:
int id;
};
template <class T, int size = 10>
class D1 {
public:
D1() : index(0) {};
D1(const D1&) {};
virtual ~D1() {};
T vt[size];
void push(T* i) {
vt[index] = *i;
index++;
}
void exvet() {
for(int i = 0; i<index; i++)
cout << "vt[" << i << "] = " << vt << endl;
}
void vetsize() {cout << size << endl;}
private:
int index;
};
int main()
{
D1<int, 5> od4; //First with int.
for(int i = 0; i<5; i++)
od4.push(&i); //<- Inserting ints in the array. Fine.
od4.exvet(); //<- Shows the ints int the array, Fine too.
D1<B, 10> od5; //Now with the programmer defined type.
for(int i = 0; i<10; i++)
od5.vt.dummy(); //<- !!!!!!!HERE!!!!!! It compiles and run!
}
In my mind, when I declared "D1<B, 10> od5", I allocated memory for 10
elements of the type "B" -- as I did when I defined for the int type
"D1<int, 5> od4". BUT, in the "int" case, I had to insert data in the
vector "vt" of the template in order to properly access it. Therefore,
how could I access the members of "od5" if I did not insert any class
"B" object in the respective array "vt"?
Wouldn't a more suited way for this case be:
D1<B, 10> od5;
for(int i = 0; i<10; i++)
od5.push(new B); //Inserting class B objects.
for(int i = 0; i<10; i++)
od5.vt.dummy(); //Accessing members of the objects inserted.
How about it??
I appreciate any comment, suggestion, etc.
Thank You!
Marcelo