Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:58:12+00:00 2026-06-16T04:58:12+00:00

I am writing a filesystem abstraction in C++, with the following inheritance hierarchy: [Node]

  • 0

I am writing a “filesystem” abstraction in C++, with the following inheritance hierarchy:

 [Node]
   ^
   |
   +----[Dir]
   |
   +----[File]

Where Node defines all the behavior identical to both (Name, time last modified, etc.) however, I have a Node method called getParent() that returns a type Dir *. This works fine, because although Dir.h obviously needs to know the implementation specification in Node.h, Node.h doesn’t need to know about what’s in Dir.h so I can use a forward declaration. Great.

However, I recently decided to add in multiple inheritance so I can support “snapshots” of the filesystem at a certain time. These are read-only versions of the “live” Node File and Dir classes, and since the live versions can be read from as well as written to, I have each live version inherit from its snapshot dual:

[NodeSnapshot] <------ [Node]
    ^                    ^
    |                    |
    +---[DirSnapshot]<---+---[Dir]
    |                    |
    +---[FileSnapshot]<--+---[File]

Therefore, Dir inherits from both Node and DirSnapshot, and File inherits from both FileSnapshot and Node. Everything looks good to be so far, until we get to the declaration of getParent(). In NodeSnapshot, I return a DirSnapshot *. No problem, I can use a forward declaration again. However, in Node, I want to return Dir *. I, as a programmer, know that Dir is a subtype of DirSnapshot, however the compiler has no way of knowing this because a forward declaration doesn’t have any of this useful information embedded in it.

Is it possible to inform the compiler that this forward declaration is a subclass and therefore it shouldn’t tell me that the return type of Dir::getParent() does not covary with that of DirSnapshot::getParent()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:58:14+00:00Added an answer on June 16, 2026 at 4:58 am

    It is possible to implement/emulate return type covariance without any language support, though solutions tend to be verbose. On the other hand, mutually recursive definitions are no problem. One needs to use non-virtual public (inline) functions that call virtual private functions. It is a useful technique, some even argue that all interfaces should be implemented like this.

    Here’s an example:

    // forward declare classes
    class A;
    class B;
    class AA;
    class BB;
    
    // parents
    class A
    {
        virtual B* getB_impl();
    public:
        B* getB() { return getB_impl(); }
    };
    class B
    {
        virtual A* getA_impl();
    public:
        A* getA() { return getA_impl(); }
    };
    
    // kids
    class AA : public A
    {
        virtual BB* getBB_impl();
        B* getB_impl();
    public:
        BB* getB() { return getBB_impl(); }
    };
    class BB : public B
    {
        virtual AA* getAA_impl();
        A* getA_impl();
    public:
        AA* getA() { return getAA_impl(); }
    };
    
    // implement parents
    B* A::getB_impl() { return new B; }
    A* B::getA_impl() { return new A; }
    
    // implement kids
    B* AA::getB_impl() { return getBB_impl(); }
    BB* AA::getBB_impl() { return new BB; }
    A* BB::getA_impl() { return getAA_impl(); }
    AA* BB::getAA_impl() { return new AA; }
    
    // check
    A a; B b;
    A* pa; B* pb;
    AA aa; BB bb;
    AA* paa; BB* pbb;
    
    pa = b.getA();
    pb = a.getB();
    
    pa = bb.getA();
    pb = aa.getB();
    
    paa = bb.getA();
    pbb = aa.getB();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program searching a file in Mac OS filesystem. I would like
I am writing a windows filesystem minifilter driver that must fail I/O Request Packets
I'm writing some code that utilizes the boost filesystem library. Here is an excerpt
I'm writing code that runs all sorts of external commands as well as various
I'm writing backup software. I want to programmatically determine if a file has been
I am writing to hadoop file system. But everytime I append something, it overwrites
I'm writing a script that should set filesystem access rights for a folder and
I am writing a python program to mount the fuse filesystem through mount system
I have this javascript code which makes possible writing in a file { var
I'm writing custom filesystem classes for Apache SSHd. I've hit an issue when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.