C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Class recursion

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
graf.laszlo@axis.hu
Guest





PostPosted: Tue Nov 29, 2005 7:29 am    Post subject: Class recursion Reply with 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();
}

Thank you,
László

Back to top
Neelesh Bodas
Guest





PostPosted: Tue Nov 29, 2005 7:33 am    Post subject: Re: Class recursion Reply with quote



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





PostPosted: Tue Nov 29, 2005 7:55 am    Post subject: Re: Class recursion Reply with quote



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





PostPosted: Tue Nov 29, 2005 8:03 am    Post subject: Re: Class recursion Reply with 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?

Back to top
John Harrison
Guest





PostPosted: Tue Nov 29, 2005 8:51 am    Post subject: Re: Class recursion Reply with quote

[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





PostPosted: Tue Nov 29, 2005 9:07 am    Post subject: Re: Class recursion Reply with quote

[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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.