P
Piotr Sawuk
Hello, I'm new in this group and new to c++ programming.
And I already have my first question which wasn't answered
by any text-book on c++ programming I have seen so-far:
How can I define a class who's size is only known at the
time the constructor gets executed -- without the overhead
of pointer-managment (for copying) and without any additional
memory getting allocated for size or location of the variable
sized member-data?
For example, I wanted to do something like this:
class text{
public:
const char txt[];
text(char[] n):txt(n){}
}
and maybe later extend it into
class xtextublic text{
public:
int colour;
xtext(char[] n, int c):colour(c){text(n);}
}
I expected the compiler to create something alike a struct
struct xtext{
int colour;
.... //other stuff neccessary for the class xtext or its parent-class
char txt[TEXTSIZE];
}
with TEXTSIZE being inserted at run-time by the constructor.
But all I got was an error about "incompatible types in
assignment char* to const char[0]".
Do I really need to use such tricks as I have seen in the
sources of XFree86, like for example
class text{
char txt;
text* create_text(char* t){
text* r=(text*)malloc(strlen(t)+sizeof(text)-1);
strcpy(&r->txt,t);
return r;
}
~text(){free this;}
}
or whatever neccessary to override the usual allocation of
memory for the object?
And I already have my first question which wasn't answered
by any text-book on c++ programming I have seen so-far:
How can I define a class who's size is only known at the
time the constructor gets executed -- without the overhead
of pointer-managment (for copying) and without any additional
memory getting allocated for size or location of the variable
sized member-data?
For example, I wanted to do something like this:
class text{
public:
const char txt[];
text(char[] n):txt(n){}
}
and maybe later extend it into
class xtextublic text{
public:
int colour;
xtext(char[] n, int c):colour(c){text(n);}
}
I expected the compiler to create something alike a struct
struct xtext{
int colour;
.... //other stuff neccessary for the class xtext or its parent-class
char txt[TEXTSIZE];
}
with TEXTSIZE being inserted at run-time by the constructor.
But all I got was an error about "incompatible types in
assignment char* to const char[0]".
Do I really need to use such tricks as I have seen in the
sources of XFree86, like for example
class text{
char txt;
text* create_text(char* t){
text* r=(text*)malloc(strlen(t)+sizeof(text)-1);
strcpy(&r->txt,t);
return r;
}
~text(){free this;}
}
or whatever neccessary to override the usual allocation of
memory for the object?