Sponsored Content
Top Forums Programming C++ class definition with a member of the same class Post 302430729 by Corona688 on Friday 18th of June 2010 01:15:48 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
__gnu_pbds(3)						     Library Functions Manual						     __gnu_pbds(3)

NAME
__gnu_pbds - SYNOPSIS
Classes struct associative_tag class basic_branch struct basic_branch_tag class basic_hash_table struct basic_hash_tag struct basic_invalidation_guarantee struct binary_heap_tag struct binomial_heap_tag class cc_hash_max_collision_check_resize_trigger class cc_hash_table struct cc_hash_tag struct container_error struct container_tag struct container_traits struct container_traits_base struct container_traits_base< binary_heap_tag > struct container_traits_base< binomial_heap_tag > struct container_traits_base< cc_hash_tag > struct container_traits_base< gp_hash_tag > struct container_traits_base< list_update_tag > struct container_traits_base< ov_tree_tag > struct container_traits_base< pairing_heap_tag > struct container_traits_base< pat_trie_tag > struct container_traits_base< rb_tree_tag > struct container_traits_base< rc_binomial_heap_tag > struct container_traits_base< splay_tree_tag > struct container_traits_base< thin_heap_tag > class direct_mask_range_hashing class direct_mod_range_hashing class gp_hash_table struct gp_hash_tag class hash_exponential_size_policy class hash_load_check_resize_trigger class hash_prime_size_policy class hash_standard_resize_policy struct insert_error struct join_error class linear_probe_fn class list_update struct list_update_tag class lu_counter_policy class lu_move_to_front_policy struct null_node_update struct null_type struct ov_tree_tag struct pairing_heap_tag struct pat_trie_tag struct point_invalidation_guarantee class priority_queue struct priority_queue_tag class quadratic_probe_fn struct range_invalidation_guarantee struct rb_tree_tag struct rc_binomial_heap_tag struct resize_error class sample_probe_fn class sample_range_hashing class sample_ranged_hash_fn class sample_ranged_probe_fn class sample_resize_policy class sample_resize_trigger class sample_size_policy class sample_tree_node_update struct sample_trie_access_traits class sample_trie_node_update struct sample_update_policy struct sequence_tag struct splay_tree_tag struct string_tag struct thin_heap_tag class tree class tree_order_statistics_node_update struct tree_tag class trie class trie_order_statistics_node_update class trie_prefix_search_node_update struct trie_string_access_traits struct trie_tag struct trivial_iterator_tag Typedefs typedef void trivial_iterator_difference_type Functions void __throw_container_error () void __throw_insert_error () void __throw_join_error () void __throw_resize_error () Detailed Description GNU extensions for policy-based data structures for public use. Author Generated automatically by Doxygen for libstdc++ from the source code. libstdc++ Tue Jun 10 2014 __gnu_pbds(3)
All times are GMT -4. The time now is 09:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy