2

I am making linked list of sectors. A sector can either be of type directorySector or userSector. The first sector is always a directory sector, and the rest are not known until run time. How would I link a directorySector to a userSector? I can't have the example code because a sector of type directorySector does not always point to another directorySector. Thanks in advance!

struct directorySector{
directorySector *ptr;
};
JohnnyBoy
  • 69
  • 7
  • 2
    Create a `class Sector { Sectror* ptr };`, have `directorySector` and `userSector` derive from that. – Igor Tandetnik Aug 21 '14 at 19:03
  • ^^ Yep. And if the sectors need to provide some funtionality through their pointers, pass it via an abstract virtual function on the Sector class . Something like: `virtual [type] function([args]) = 0;` – Conduit Aug 21 '14 at 19:07
  • Thank you Igor, I tried that the first time but I must have done it wrong. Tried it again and it works. Thank you very much! – JohnnyBoy Aug 21 '14 at 19:13
  • Oh i found another problem. so I did what you said, but I have the following code, when i do sect.FRWD->free, it says that the parent does not have a member named free. Free is in the child userDirectory struct. How do I reference that one and not the parent? dirSect sect; sect.FRWD = new userSect; sect.FRWD-> free; – JohnnyBoy Aug 21 '14 at 20:32

1 Answers1

2

You can use polymorphism to point to the children class with a pointer of the base class. you will need to make sure you keep track of the type so you don't mess with the types and access an invalid data type. You might need to cast the values to go back to the children class.

class sector
{
   sector* sectorPtr;
};

class directorySector : public sector
{
   /*WhatEver*/
};

class userSector : public sector
{
   /*Whatever*/
};

int main()
{
   sector mySector;
   directorySector myDirectorySector;
   userSector myUserSector;
   mySector.sectorPtr = &myDirectorySector;
   mySector.sectorPtr = &myUserSector;

   return 0;
}

More info:

http://www.cplusplus.com/doc/tutorial/polymorphism/

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • 1
    Missing `;` after class definition. Use public inheritance. Fix typo `sserSector` -> `userSector`. – Jarod42 Aug 21 '14 at 19:34
  • @krillgar - next time, reject the suggested edit with a custom reason, then visit the question and edit it appropriately. Potential editors need to know that altering coding style is not acceptable. – MattDMo Aug 21 '14 at 19:53
  • Alright, thanks for the heads up. That wasn't a situation that I haven't run across before. – krillgar Aug 21 '14 at 19:54
  • Thanks for the changes! Sorry about that! – Jose Monsalve Diaz Aug 21 '14 at 20:40
  • I get the same problem as the top solution. when i use mySector.sectorPtr-> number; ( assuming that userSector has a member named number), I get the error "Sector Does not have a member named number" it is still searching in the base class. I need it to dereference the number data member in userSector. – JohnnyBoy Aug 21 '14 at 21:02
  • @krillgar @MattDMo For the sake of arguing (because I do not agree this is altering coding style), here is why I capitalized class names. The main reason is that it makes reading easier because it adds a color to class names. The second one is that the post had typos anyway. Look at `Sector mySector;` in the original post : the type name is capitalized. Third and least (because this is not a valid reason on itself) : most of the code I read on SO has capitalized class names. Anyway, I just think the re-edit was not necessary, but I will not complain. After all, John Tungul does not capitalize. – Nelfeal Aug 21 '14 at 21:35
  • @JohnTungul Take a look at [this](http://en.cppreference.com/w/cpp/language/virtual) (and maybe [this](http://stackoverflow.com/questions/2391679/can-someone-explain-c-virtual-methods) too, if you want a more friendly explanation) – Nelfeal Aug 21 '14 at 21:37