linkedlist pointer in class

This is a discussion on linkedlist pointer in class within the C++ Programming forums, part of the General Programming Boards category; Hi.. I have a sockclass that will be used in linkedlist separetly and also in another class (aclass) with its ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    Question linkedlist pointer in class

    Hi..

    I have a sockclass that will be used in linkedlist separetly and also in another class (aclass) with its public members.

    Code:
    class sclass {
    public:
    
    int member;
    int sth;
    SOCKET s;
    
    sclass *next;
    }

    Code:
    class aclass : public sclass {
    public:
    char *p;
    
    aclass *next;
    }
    Now what would be the best way to use *next pointer in both cases for linked list? To leave it as it is, or to put void *pointer in sclass and then point to sclass or aclass (if I will use aclass with public members of sclass), or just leave it as it is? I want my code to be professional, so Im asking for your advice..

    What do you think would be the best way to do it?

    Thanks for help
    Kind regards
    Last edited by l2u; 05-09-2006 at 04:13 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Don't put a next variable in aclass. Leave the next variable in sclass as an sclass*. This is what polymorphism is. When you add a new instance to the list, you use new sclass or new aclass, and you can save the result as an sclass*.

    Just make sure your sclass has a virtual destructor, and that aclass can access the next value somehow (currently next is private). Creating a protected or public access method for the next item is probably the best way.

    Whenever you want to use an aclass stored in the sclass*, you should be calling virtual methods in the base class that may or may not be overriden in the derived class (or calling non-virtual methods in sclass that call other virtual methods).

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    What exactly are you trying to do there? That's not how inheritance is used.. you have no public or protected interface for the base class, so your derived class will not have any access to its members

    I want my code to be professional, so Im asking for your advice
    I guess if you really want to it to look professional, you should use the STL linked list
    Last edited by Bench82; 05-09-2006 at 03:49 PM.

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Yes it was my mistake to put there private instead of public.


    Quote Originally Posted by Bench82
    I guess if you really want to it to look professional, you should use the STL linked list
    Why would STL linked list be more "professional?

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved
    Just make sure your sclass has a virtual destructor
    Why would I need a virtual destructor?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Why would I need a virtual destructor?
    You need a virtual destructor in order to delete a derived class instance through a pointer to the base class. Since you would be storing all instances of aclass in the list through sclass pointers, the virtual destructor makes sure that when you clean them up they are destroyed properly.

    >> Why would STL linked list be more "professional?
    You shouldn't write from scratch code that is already available to you, especially when that code is part of the standard library that is extremely widely used and tested. If this isn't part of a learning or teaching exercise, you really should use standard containers instead of your own versions in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  3. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21