Topic: Variable Length Class
Author: schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann)
Date: 22 Jul 1994 18:01:37 GMT Raw View
In article <30atdp$457@cstatd.cstat.co.za>, vincer@iaccess.za (Vincent Risi) writes:
|>
|> What is the best way to declare variable length classes? Given the
|> following code snatch, what are the alternatives? You do not want a static
|> or global instance variable of the class to be declared, it should always be
|> declared as a pointer. Is there a more elegant (less error prone) mechanism
|> to handle this sort of class structure?
|>
|> typedef class tA* pA;
|> typedef unsigned short ushort;
|>
|> class tA
|> {
|> ...
|> ushort Size;
|> char Data[1];
|> tA(void):Size(0){}
|> public:
|> static pA Create(ushort aSize);
|> };
|>
|> pA tA::Create(ushort aSize)
|> {
|> pA A = pA(new char[aSize+sizeof(tA)-1]);
|> ...
|> A->Size = aSize;
|> return A;
|> }
|>
|> ...
|> pA A = tA.Create(200);
|> ...
|>
|>
|> Vince Risi
|> ==========
I'm not sure what you intend to do with this class (and what you
mean by 'variable length classes') When you know the size that a
tA should have at the point of construction, you can use a
templateclass:
template<ushort Sz> class tA {
char Data [Sz];
public:
ta() {};
ushort Size () { return Sz; }
};
...
tA<200> ta200;
tA<200> *pa = new tA<200>;
...
Ulf Schuenemann
--------------------------------------------------------------------
Ulf Sch nemann
Institut f r Informatik, Technische Universit t M nchen.
email: schuenem@informatik.tu-muenchen.de
WWW: http://hphalle2/~schuenem (currently not available from outside)
Devil's advocate
Author: vincer@iaccess.za (Vincent Risi)
Date: 17 Jul 1994 09:25:13 GMT Raw View
What is the best way to declare variable length classes? Given the
following code snatch, what are the alternatives? You do not want a static
or global instance variable of the class to be declared, it should always be
declared as a pointer. Is there a more elegant (less error prone) mechanism
to handle this sort of class structure?
typedef class tA* pA;
typedef unsigned short ushort;
class tA
{
...
ushort Size;
char Data[1];
tA(void):Size(0){}
public:
static pA Create(ushort aSize);
};
pA tA::Create(ushort aSize)
{
pA A = pA(new char[aSize+sizeof(tA)-1]);
...
A->Size = aSize;
return A;
}
...
pA A = tA.Create(200);
...
Vince Risi
==========