| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | problem in c++ |
> Recently, I want to use Borland C++ 4.0 to write a oop
> program. When I tried to initialize a pointer of array of
> structure with a series of data, the compiler does not
> permit that.
Actually, it does. The problem is with constructing individual elements of the array.
> The pointer is a data member of a class, the
> compiler told me to use something like constructor to do
> it. But I don't know how. Can you help me? :)
Sure.
> the program look likes this:
> struct unit {char ch;int i};
> class dim
> {
> unit *x;
> };
> x=new unit[]={...};
Hang on. You shouldn't have any problem with this - there's no constructor
at all, and therefore the compiler already provides a default (do nothing)
constructor, so this as it stands will work fine once you remove the
"={..};" bit. This is precisely the same as the C statement:
x=malloc(sizeof(struct unit)*elements_in_the_array);
I suspect that your program doesn't really look like the above, and instead
you have something like...
class unit
{
public:
unit( char _ch, int _i ) : ch(_ch), i(_i) {}
private:
char ch;
int i;
};
x = new unit[whatever];
If the compiler is complaining about constructors, it is because the type
of array being constructed does not involve a class that has a default
constructor.
Now, the fundamental problem is that a (default) constructor that takes no
arguments needs to be called for each element in the array being created.
If the array was static or auto rather than dynamically allocated, you
could use a copy constructor and add:
unit(const unit & u ) : ch( u.ch ), i( u.i) {}
.. or use the compiler generated copy constructor which does much the same
thing (in this case) by doing a memberwise copy.
then you could declare the array thus:
unit x[whatever] = {
unit( 'a', 1 ),
unit( 'b', 2 ),
// and so on.
};
but if the array is dynamic, you *need* to provide a default constructor to
initialise each element.
One (and not the only) way to do this is to derive a class:
class unit_initialiser : public unit
{
public:
unit_initialiser()
: unit( 'a', 1 )
{}
};
Note that this *does* provide a default initialiser, so you can do:
x = new unit_initialiser[whatever];
and it'll work just fine. Any element of the array x can, because
unit_initialiser is derived from class unit, be handled just like a 'unit'
since - by definition - it IS one.
If you want to init elements of the array with different values, you can
provide some static members in class unit_initialiser to do so, whether
this be in the form of an array of initialiser values and a static index
that is incremented each time the constructor is called, or whether there
is some pattern in it that can use the static member directly. For example:
class unit_initialiser : public unit
{
public:
unit_initialiser()
: unit( char('a'+stater_value), 1+starter_value )
{ ++starter_value; }
static int starter_value;
};
int unit_initialiser::starter_value = 0;
In this case, an array allocated this way:
x = new unit_initialiser[5];
would have an array containing:
x[0] = { 'a', 0 }
x[1] = { 'b', 1 }
x[2] = { 'c', 2 }
x[3] = { 'd', 3 }
x[4] = { 'e', 4 }
Hope this helps,
David
---
* Origin: Unique Computing, Melbourne, Australia (3:632/348)SEEN-BY: 50/99 620/243 623/630 632/103 348 998 633/371 634/384 388 635/301 SEEN-BY: 635/502 503 544 727 636/100 639/100 711/401 409 410 430 510 807 808 SEEN-BY: 711/809 932 934 712/515 713/888 714/906 800/1 7877/2809 @PATH: 632/103 348 635/503 50/99 711/808 809 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.