473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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ó

Nov 29 '05 #1
5 4165
graf.las...@axis.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.

Nov 29 '05 #2
Thank you.
Neelesh Bodas írta:
graf.las...@axis.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.


Nov 29 '05 #3
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?

Nov 29 '05 #4
gr*********@axis.hu wrote:
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
Nov 29 '05 #5
gr*********@axis.hu wrote:

[...]
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

Nov 29 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
3
by: SamIAm | last post by:
Hi I have a form named form1. I have a class called class1. I want to call a recursive method in class1 called MyMethod() I would like log info to a listbox, listbox1, on the form as MyMethod...
9
by: Christian E. Böhme | last post by:
Hello all, I ran into a little problem with recursive templates that I am not sure what it has to do with, essentially, since I am currently limited in my access to compilers (namely GCC 4.1)...
2
by: Dom Jackson | last post by:
Hello - I have a problem where I need to test some numeric code using a variety of built-in integer types: obj_type1 = obj_type2 OP obj_type3; // is obj_type1 correct? If I test with 10...
20
by: athar.mirchi | last post by:
..plz define it.
6
by: Patient Guy | last post by:
I am a newcomer to using PHP but not to programming (C, C++, Javascript). I am playing around with classes and wanted to make a function that has a method simply for producing either plain text...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.