| View previous topic :: View next topic |
| Author |
Message |
graf.laszlo@axis.hu Guest
|
Posted: Tue Nov 29, 2005 7:29 am Post subject: Class recursion |
|
|
Hi,
Can I define a class A, wich has a private array member with base type
A?
class A {
private:
char* name;
A a[];
public:
A();
A(char* p_name);
~A();
}
Thank you,
László
|
|
| Back to top |
|
 |
Neelesh Bodas Guest
|
Posted: Tue Nov 29, 2005 7:33 am Post subject: Re: Class recursion |
|
|
graf.las... (AT) axis (DOT) hu wrote:
| Quote: | Hi,
Can I define a class A, wich has a private array member with base type
A?
class A {
private:
char* name;
A a[];
public:
A();
A(char* p_name);
~A();
}
|
No.'a' can be array of pointers to A
A* a[];
Alternatively, 'a' can be static
static A a[]; // define outside the class.
|
|
| Back to top |
|
 |
graf.laszlo@axis.hu Guest
|
Posted: Tue Nov 29, 2005 7:55 am Post subject: Re: Class recursion |
|
|
Thank you.
Neelesh Bodas írta:
| Quote: | graf.las... (AT) axis (DOT) hu wrote:
Hi,
Can I define a class A, wich has a private array member with base type
A?
class A {
private:
char* name;
A a[];
public:
A();
A(char* p_name);
~A();
}
No.'a' can be array of pointers to A
A* a[];
Alternatively, 'a' can be static
static A a[]; // define outside the class.
|
|
|
| Back to top |
|
 |
graf.laszlo@axis.hu Guest
|
Posted: Tue Nov 29, 2005 8:03 am Post subject: Re: Class recursion |
|
|
I redefined it as you indicated and I added a new constructor but I
need one more thing.
The class looks like this:
class A {
private:
char* name;
A* a[];
public:
A();
A(char* p_name);
A(char* p_name, A* p_a);
~A();
}
How should implement the constructors?
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Tue Nov 29, 2005 8:51 am Post subject: Re: Class recursion |
|
|
[email]graf.laszlo (AT) axis (DOT) hu[/email] wrote:
| Quote: | I redefined it as you indicated and I added a new constructor but I
need one more thing.
The class looks like this:
class A {
private:
char* name;
A* a[];
public:
A();
A(char* p_name);
A(char* p_name, A* p_a);
~A();
}
How should implement the constructors?
|
Surely that depends on what you want to do?
Part of the problem here is that you are using illegal syntax.
class A {
private:
char* name;
A* a[];
This is not legal C++. Your compiler might be accepting it but your
compiler is wrong. When you declare an array in C++ you have to say how
big it is. This is legal
class A {
private:
char* name;
A* a[10];
Now I'm guessing, but I would say that Neelesh Bodas misunderstood what
you want. I think that problably you want a dynamic array of A objects
inside your A class. That is perfectly legal, and you write it like this
class A {
private:
char* name;
A* a;
In the constructor you dynamically allocate however many A objects you
want. Like this
a = new A[n];
where n is the number of A objects you want.
John
|
|
| Back to top |
|
 |
Mateusz Loskot Guest
|
Posted: Tue Nov 29, 2005 9:07 am Post subject: Re: Class recursion |
|
|
[email]graf.laszlo (AT) axis (DOT) hu[/email] wrote:
| Quote: |
[...]
How should implement the constructors?
|
Are you going to implement tree-like structure/collection with
recursively nested class?
If you are, then I think you should drop that approach and take a look
at Composite design pattern.
Cheesr
--
Mateusz Loskot
http://mateusz.loskot.net
|
|
| Back to top |
|
 |
|