Visit Our UNIX and Linux User Community


C++ class definition with a member of the same class


 
Thread Tools Search this Thread
Top Forums Programming C++ class definition with a member of the same class
# 1  
Old 06-18-2010
C++ class definition with a member of the same class

Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento)

Code:
typedef struct elemento
   {
      char name[40];
      char value[100];
      List<struct elemento> ltElementos;
   } s_elemento;

typedef List<s_elemento> ListElementos;

Convert to:
Code:
   public class Elemento
   {
       string name;
       string value;
       List<Elemento> ltElemento;
       
       ......
   
   }

If i compile that, the error is:
Code:
field `Elemento' has incomplete type

May someone help me?
# 2  
Old 06-18-2010
Several things about this post make me think you're asking something wrong:

  • Your "C" code includes a template. That's not C. Are you using some extension or something?
  • Your "C++" code includes "public class ..." - the public keyword is misplaced there, and should generate its own warning before the one you point out.
  • Your C++ code includes a List, not a list.


Is this really C and C++ code, respectively? Also, is the error you show the very first error the compiler generates?

You should be able to declare what you did. For example, this:

Code:
#include <list>

class Thing {
    std::list<Thing> stuff;
};

Compiles just fine for me, even with the -Wall, -Wextra and -pedantic flags.

Could you give a short (but full and compilable) example that demonstrates your problem?
# 3  
Old 06-18-2010
Hi, and thanks for your help.

Oh, yes, of course. I was wrong and have mixed Java code with my C++ code. I was trying in Java to compare and my post was wrong.

About your questions:

1. I am programming with C++ with "typedef structs" and i want to try it with C++ class.
2. Yes, "public" is wrong.

Then, i 'll try what you comment.

So, thanks, and sorry by my mistakes.
# 4  
Old 06-18-2010
Quote:
Originally Posted by pogdorica
Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class?
I don't think you can. Think about it: Wouldn't that class inside the class, also have a class inside the class? Which would also have a class inside the class. etc. etc. etc. If you want different things to have different members, they have to be different structures.

This doesn't even work in C. Observe:
Code:
typedef struct whatever
{
    int a;
    struct whatever s;
} whatever;

will give an error since you can't nest that way.

You can cheat by using a pointer. The compiler will let you make pointers to incomplete types since a pointer's always the same size.

Code:
typedef struct linkedlist
{
    int payload;
    struct linkedlist *next;
} linkedlist;

That way it only allocates the size of the pointer, not the entire struct, avoiding the infinite recursion. This means you have to explicitly allocate memory to it later to use it, of course.
# 5  
Old 06-19-2010
A rule of thumb ...

Always keep in mind that you are bound to get an error generated by the compiler (irrespective of the compiler's origin) that if its not following the below rule (although unstated anywhere in books -its my own logic to predict a code's compilability ) -

"unless you are able to calculate the size of a class / struct; manually (by simple addition of all the member's size); its not gona compile."

Now apply this rule and try calculating the size off the class, which you have define. Are you not going into an infinite loop of calculation?

The above definition are not gona work. Smilie

You may wish to call it praveen's compilability rule Smilie

Any comments?

---------- Post updated at 11:17 AM ---------- Previous update was at 11:03 AM ----------

Quote:
Originally Posted by Corona688
I don't think you can. Think about it: Wouldn't that class inside the class, also have a class inside the class? Which would also have a class inside the class. etc. etc. etc. If you want different things to have different members, they have to be different structures.

This doesn't even work in C. Observe:
Code:
typedef struct whatever
{
    int a;
    struct whatever s;
} whatever;

will give an error since you can't nest that way.

You can cheat by using a pointer. The compiler will let you make pointers to incomplete types since a pointer's always the same size.

Code:
typedef struct linkedlist
{
    int payload;
    struct linkedlist *next;
} linkedlist;

That way it only allocates the size of the pointer, not the entire struct, avoiding the infinite recursion. This means you have to explicitly allocate memory to it later to use it, of course.

Absolutely true!!!
However, here again "The Rule" works well to predict what's compilable and what's not.

In the later example, the pointer :
Code:
struct linkedlist *next;

scores 4-bytes /8-bytes respectively on 32-bit / 64-bit systems and hence enabling you to calculate the size of the struct.

However, its simple examples however the real advantages come when you have got complicated scenarios and / or working in environments where you can not afford to compile more frequently to see the results (like an outdated embedded programming environment or with cross compilers on 486 machine hosts Smilie
# 6  
Old 06-19-2010
Corrona688 and Praveen_218: What's really being asked about here is using a class as a template parameter within itself (despite what the title may suggest). You can do that - not sure what restrictions there are on that, but it's definitely possible.

I'm sure if the OP has a specific problem, they'll post a short snippet of code that demonstrates it.
# 7  
Old 06-19-2010
get rid of typedef.
Code:
struct elemento {
    list<elemento> v;
};

not sure why you'd want a list of elementos inside an elemento though.
I wouldn't do it.

also, if using C++ prefer the std::string over char *

the string an STL classes are very fast indeed and make life much easier.

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

2. Programming

Restricting member of a class non-inheritable in C++

There is base class B, and two derived classes D1 and D2 derived from Base. Base class B, have two data members ( public or protected or private or if any). D1 should inherit both these data members, and D2 should be deriving only one member from Base class. Is this kind of design possible without... (1 Reply)
Discussion started by: techmonk
1 Replies

3. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

4. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

5. Programming

static use for class inside the same class c++

Hi, I believe the next code is wrong: class Egg { Egg e; int i; Egg(int ii=0) : i(ii) {} }; because you would end up with an endless definition (memory allocation) of Egg objects, thus int i. Ok, so God Eckel proposes for a singleton: class Egg { static Egg e; int... (5 Replies)
Discussion started by: xavipoes
5 Replies

6. Programming

Writing C++ class and member functions

I have the following class and thought that when I call the set command to set a member, I always use value. Would that be fine? class ModMisfit { protected: Real dtau; Real mdacc; Real mindist; bool hw; Source** src; public: void ... (7 Replies)
Discussion started by: kristinu
7 Replies

7. UNIX for Dummies Questions & Answers

car class (not school class)

im just trying to have some fun and kill some time writing a c++ program that has a person type in a car make and model then gives them a year and a price. or something like that. i always have problems getting it goin but once the ball is rolling im usually pretty good. anyone wanna help me out? ... (1 Reply)
Discussion started by: rickym2626
1 Replies

8. Programming

Handling a signal with a class member function

Hello, i am using the sigaction function to handle the SIGCHLD signal.Is it possible to use a class member function as the handler function (the sa_handler member of the sigaction structure)? The function's signature is: void (*sa_handler)(int);so i don't think i can use a static member function... (2 Replies)
Discussion started by: Zipi
2 Replies

9. UNIX Desktop Questions & Answers

It's first class!

Hi..!!!You have a very nice web-site! Good work!I am also webmaster and I like this one!Thank you! (1 Reply)
Discussion started by: Hardan
1 Replies

10. UNIX Desktop Questions & Answers

It's first class!

This is a great sight! Excellent site, added to favorites!! I am also webmaster and I like this one!Thank you! (0 Replies)
Discussion started by: Leksej
0 Replies

Featured Tech Videos