| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | problem in c++ |
GC> Recently, I want to use Borland C++ 4.0 to write a oop program. When I
GC> tried to initialize a pointer of array of structure with a series of data,
GC> the compiler does not permit that. The pointer is a data member of a class,
GC> the compiler told me to use something like constructor to do it. But I
GC> don't know how. Can you help me? :)
GC> the program look likes this:
GC> struct unit {char ch;int i};
GC> class dim
GC> {
GC> unit *x;
GC> };
GC> x=new unit[]={...};
Suggestion: Before you give us a problem, try to write a tiny
compilable program that clearly demonstrates the problem. Often,
you'll find the solution yourself while you do this.
And if that doesn't work, the tiny compilable program is MUCH
easier for us alleged experts to troubleshoot...
The following mess compiles ok under BC++4.02, and shows a couple
of ways of initializing class data.
====
struct Unit { char ch; int i; };
class Dim1 /* this class doesn't define a constructor, it uses C++'s default
constructor (with no parameters)*/
{
public: // optional. But if omitted, all members will be PRIVATE!
Unit x; // data member
void setch(char C); // public member functions for setting data members
void seti (int I);
};
class Dim2 // this class uses a constructor that initializes the class's data
{
public:
Unit x; // data member
Dim2(char C, int I); // constructor with parameters
};
// fully define the class functions here
void Dim1::setch(char C) {x.ch = C;}
void Dim1::seti (int I) {x.i = I;};
Dim2::Dim2(char C, int I) {x.ch = C; x.i = I;}
// this is the real program
int main(void)
{
/* Define a class, and a pointer to a class, for both Dim1 and Dim2.
Note the Dim2 variable definition "realdim2" contains the
intial x values
in its constructor, but the Dim2 pointer definition doesn't - this is
because dpointer2 is only a pointer, so it ain't got no room for the
contents of class Dim2. The contents happen later, when "new
Dim2('d',4)"
is called. */
Dim1 realdim1; // this is a real occurrence of Dim1
Dim2 realdim2('c', 3); /* this is a real Dim2,
and it's initialized with 'c' and 3 */
Dim1 *dpointer1; // this is only a pointer to a not yet real Dim1
Dim2 *dpointer2; // ditto for Dim2
realdim1.x.ch = 'a'; // initialize data members of Dim1 directly
realdim1.x.i = 1;
// Or alternatively,
realdim1.setch('b'); // call Dim1's set-data functions. Useful if you
realdim1.seti(2); // want ch and i to be private members of class Dim1.
dpointer1 = new Dim1; // Create a new Dim1, pointed to by dpointer1
dpointer1->x.ch = 'b'; // and initialize its data members directly.
dpointer1->x.i = 2;
dpointer2 = new Dim2('d', 4); /* Create (and initialize the data
member of) a new Dim2. */
return 1;
}
====
Cheers
--- PPoint 1.88
* Origin: Silicon Heaven (3:711/934.16)SEEN-BY: 711/809 934 @PATH: 711/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™.