Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Dynamic_cast from private derived to public base

Dynamic_cast from private derived to public base

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by
    #1

    I searched details about inheritance and dynamic_cast but I haven't figured out if this is possible and, if it is, any side-effects.

    I have a base class which has some public attributes. The base class is not a "proper" class, it's actually a struct, which has all its members public by default.
    @struct base{
    // public:
    int a;
    float b;

    // initializing through constructor
    base(){
    a=0;
    b=0.0;
    }
    }@

    Now, when I create a derived class I want the base class attributes not to be accessed directly because I have to make validity checks and other things whenever they should be modified. For this purpose I have public methods which would do the work, and I derive the base class as private.
    @class DerivedClass : private base{
    public:
    setVarA(int value){ a = (value>0)?value:0; }
    setVarB(float value){ b = (value<5.0)?value:0; }

    //remember that a and b are now private
    /**************
    private:
    int a;
    float b;
    ***************/
    }@

    I need base to have its members public because it contains the basic structure of an object, and it's by far more convenient to use such a simple struct when passing it as function parameters (I often need to) instead of passing a whole class with all methods and any other members.

    The final questions are:
    @DerivedClass * MyDerivedClass = new DerivedClass;
    base * casted_class = dynamic_cast<base *>(MyDerivedClass); // is it allowed ?@

    if I perform a dynamic_cast on the derived class to the base class, will it work just fine even if the derived class has private members instead of public?
    If this is allowed to do, does that mean that I can cast any derived class to one of its base classes regardless of the inheritance access type? (f.e. casting protected to private or to public)
    Are there any side-effects ?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lutzhell
      wrote on last edited by
      #2

      Hi,

      at first i have to say that your assumption about passing objects as function parameters is correct but there exists a workaround for the overhead:

      Pass the object by reference:

      @void function(base &anObject)@

      if the object should not be changed in function pass it as const reference:

      @void function(const base &anObject)@

      At second i have to say since it breaks encapsulation it is always a bad design to have public class members unless you have a real good reason to do so.

      When i understand you correctly your base class only function is to provide a base for derived classes. If only the derivates shall have access to base's members just declare them as "protected".

      In this case it is also a good idea to declare the constructor of your base class as "protected" so it could never be the case that an object of type base comes to life accidentally.

      Here is some information about type casting in C++:

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

      Here is a link to some more information about private inheritance:

      http://www.parashift.com/c++-faq/private-inheritance.html

      1 Reply Last reply
      0
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by
        #3

        Thank you very much for replying ;)
        I have figured it all on my own since then, but I didn't think about using references which could have saved me some work. Also, in the meantime the whole application design has changed and I totally left out this base-derived approach.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved