Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your example code contains at least 4 syntax errors, so it's hard to judge what you're trying to do.</p> <p>And it logically won't work either. You have a class <code>AImpl</code> that inherits <code>A</code>, then a member function that takes an <code>A</code> and appears to try to dynamic-cast it to an <code>AImpl</code>. But it isn't an <code>AImpl</code>, because it's just an <code>A</code>, as that is how the parameter is declared. If you were to pass an instance of <code>AImpl</code> to that function, it would be sliced down to just an <code>A</code>.</p> <p>You could make it a reference or pointer to an <code>A</code>, and then it could be an <code>AImpl</code>. Dynamic casts are only ever of use on references or pointers.</p> <p>Down-casting is used when we have a variable or parameter that has the static type of a base class but we logically know that it is (or might be) of a derived class. It is avoided wherever possible because it means that the compilation process is not able to completely check the type-correctness of the program, i.e. the answer to the question "are you trying to put a square peg into a round hole" cannot be fully answered until runtime.</p> <p><strong>Update after question was edited</strong></p> <p>It sounds like you want clients of your library to have access to a limited interface to an object, A, but when they pass it to a function in your library you will have access to the full interface. You could just use <code>friend</code> for this.</p> <pre><code>class A { friend class LibraryThing; void visibleToLibraryThing(); public: // ctor, etc. void visibleToAll(); }; class LibraryThing { public: void foo(A &amp;a) { a.visibleToLibraryThing(); } }; </code></pre> <p>The <code>LibraryThing</code> class can access the private members of <code>A</code>, because it is declared as a friend of <code>A</code>.</p> <p>The downside is that <code>LibraryThing</code> can access <em>everything</em> in <code>A</code>, so it means that as the author of the library, you won't be able to benefit from encapsulation. Only users of your library will.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload