Base class representation of derived classes

I tried looking for a post related to this but I had no look mincing keywords related to this together. Also the books I have on C++ haven't offered much help regarding this.

I am programming a hobby os and I am using C++ for some of the code. Hopefully this is a problem with my C++ code. In one section I have a class called filesystem. This class has virtual functions (the functions defined for these are stubs, returning an error code everytime -should they be pure virtual instead?). Anyways, I have a class derived from this called fat.
I am designing this so that I can do something like this:
[code]
filesystem *somewhere; //an array of filesystems
//point each element to a derived class
somewhere[some_number].load_file(...);
[/code]

For testing purposes this is what I am doing. Section A works, section B does not work.
[code]
//Section A
filesyste *somewhere;
somewhere = new fat;
somewhere->mount(...);
[/code]

[code]
//Section B
filesystem *somewhere;
somewhere = new fat[3];
somewhere[1].mount(...);
[/code]

I'm not so sure I am initializing the array properly. I need an array of objects that are addressable as type filesystem, but whose virtual functions point to derived class functions. I seem to remember that this was possible, but I do not remember how.

Comments

  • [code]:
    //Section B
    fat *somewhere;
    somewhere = new fat[3];
    somewhere[1].mount(...);
    [/code]:

    declare [b]somewhere[/b] as class [b]fat[/b]

    And I think you will want to make them pure virtual functions so that class filesystem doesn't have to implement them itself.

    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • How about this for a solution?

    [code]
    filesystem **local_fs;
    int some_number = (whatever);

    local_fs = new filesystem*[some_number];
    for (int counter = 0; counter < some_number; counter++)
    {
    local_fs[counter] = (address of a derived class)
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories