How do I expose a static_cast?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.ir.com.au

    How do I expose a static_cast?

    Hi,

    In the following code I get the compiler error:

    error C2243: 'static_cast' : conversion from 'class B *' to 'class A *'
    exists, but is inaccessible

    I understand why I get this error and can usually get around the situation
    by inserting a "using A::..." statement inside class B, however, due to this
    being a static cast, what is the syntax?

    --------

    class A
    {
    };

    class B : private A
    {
    };

    int main(int argc, char* argv[])
    {
    B* b;
    A* a;

    a = static_cast<A*> (b);

    return 0;
    }

    Thanks,
    David


  • Rolf Magnus

    #2
    Re: How do I expose a static_cast?

    news.ir.com.au wrote:
    [color=blue]
    > Hi,
    >
    > In the following code I get the compiler error:
    >
    > error C2243: 'static_cast' : conversion from 'class B *' to 'class A
    > *' exists, but is inaccessible
    >
    > I understand why I get this error and can usually get around the
    > situation by inserting a "using A::..." statement inside class B,
    > however, due to this being a static cast, what is the syntax?[/color]

    The syntax for what?
    The A part of B objects is private, i.e. inaccessible to the outside
    world. Therefore, you cannot convert a B pointer into an A pointer. I'm
    not sure if you meant that by "I understand why I get this error".
    Anyway, I don't understand what your question now is.
    [color=blue]
    > class A
    > {
    > };
    >
    > class B : private A
    > {
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > B* b;
    > A* a;
    >
    > a = static_cast<A*> (b);
    >
    > return 0;
    > }
    >
    > Thanks,
    > David[/color]

    Comment

    • news.ir.com.au

      #3
      Re: How do I expose a static_cast?

      Rolf,

      Thanks for your reply.

      What I meant is, I can understand that I get this error due to class A being
      unaccessible to class B. However it is possible to explictely allow members
      the be accessed with the "using" keyword.

      Eg. If I was to add the method A::Test(), it is now possible to access
      A::Test() inside class B by adding the following statement to class B:

      using A::Test();

      My question is, since the above can be done, is it possible to do the same
      for static_cast?

      I've tried the obvious of:

      using A::static_cast;

      and other variations such as:

      using A::operator static_cast;
      using A::operator static_cast<>;
      ..
      ..
      ..

      Please let me know if you still do not understand.

      Thanks,

      David


      "Rolf Magnus" <ramagnus@t-online.de> wrote in message
      news:c28fn8$2b4 $02$1@news.t-online.com...[color=blue]
      > news.ir.com.au wrote:
      >[color=green]
      > > Hi,
      > >
      > > In the following code I get the compiler error:
      > >
      > > error C2243: 'static_cast' : conversion from 'class B *' to 'class A
      > > *' exists, but is inaccessible
      > >
      > > I understand why I get this error and can usually get around the
      > > situation by inserting a "using A::..." statement inside class B,
      > > however, due to this being a static cast, what is the syntax?[/color]
      >
      > The syntax for what?
      > The A part of B objects is private, i.e. inaccessible to the outside
      > world. Therefore, you cannot convert a B pointer into an A pointer. I'm
      > not sure if you meant that by "I understand why I get this error".
      > Anyway, I don't understand what your question now is.
      >[color=green]
      > > class A
      > > {
      > > };
      > >
      > > class B : private A
      > > {
      > > };
      > >
      > > int main(int argc, char* argv[])
      > > {
      > > B* b;
      > > A* a;
      > >
      > > a = static_cast<A*> (b);
      > >
      > > return 0;
      > > }
      > >
      > > Thanks,
      > > David[/color]
      >[/color]


      Comment

      • Andrey Tarasevich

        #4
        Re: How do I expose a static_cast?

        news.ir.com.au wrote:[color=blue]
        > ...
        > Eg. If I was to add the method A::Test(), it is now possible to access
        > A::Test() inside class B by adding the following statement to class B:
        >
        > using A::Test();
        >
        > My question is, since the above can be done, is it possible to do the same
        > for static_cast?[/color]

        Formal answer - no.

        But if you really want 'B*' to be convertible to 'A*' maybe you should
        just make 'A' _public_ base class of 'B'. Although, come to think about
        it, public inheritance usually implies a lot more than a mere pointer
        convertibility. ..

        You can also use "brute force" to break through private inheritance by
        using C-style cast

        B* b;
        A* a;
        ...
        a = (A*) b;

        This will work. But this is as ugly as it ever gets.

        Maybe more elegant solution would be to introduce a member function into
        class 'B', which will return a pointer to its 'A' base

        class B : private A {
        ...
        public:
        A* get_A() { return this; }
        };

        Anyway, it would be useful if you could explain in more detail why
        exactly you need this type of functionality.

        (And would you please stop top-posting?)

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Clark Cox

          #5
          Re: How do I expose a static_cast?

          In article <nLP1c.225$IH5. 11956@news.optu s.net.au>,
          "news.ir.com.au " <davidp_au@yaho o.com> wrote:
          [color=blue]
          > Rolf,
          >
          > Thanks for your reply.
          >
          > What I meant is, I can understand that I get this error due to class A being
          > unaccessible to class B. However it is possible to explictely allow members
          > the be accessed with the "using" keyword.
          >
          > Eg. If I was to add the method A::Test(), it is now possible to access
          > A::Test() inside class B by adding the following statement to class B:
          >
          > using A::Test();
          >
          > My question is, since the above can be done, is it possible to do the same
          > for static_cast?[/color]

          No. The closest you could do would be to add some function to B, like
          the following:


          A *B::GetAPointer ()
          {
          return this;
          }

          Comment

          • Tom Plunket

            #6
            Re: How do I expose a static_cast?

            > What I meant is, I can understand that I get this error due to[color=blue]
            > class A being unaccessible to class B. However it is possible to
            > explictely allow members the be accessed with the "using"
            > keyword.[/color]

            static_cast is not a member of A or B though. There's nothing to
            use.

            Might this work?

            class B : private A
            {
            public:
            operator A&() { return *this; }
            };


            ?

            I'm not really sure why you'd want to do this though.
            Alternatively you could make A publically inherited (since you
            seem to want it to be anyway), or you could make A a sub-object
            of B (that is, B has-a A) and then offer a GetA() method... This
            seems like an odd request without knowing more context.

            -tom!

            Comment

            • Chris \( Val \)

              #7
              Re: How do I expose a static_cast?


              "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
              news:104fj94jio 1esd2@news.supe rnews.com...
              | news.ir.com.au wrote:
              | > ...
              | > Eg. If I was to add the method A::Test(), it is now possible to access
              | > A::Test() inside class B by adding the following statement to class B:
              | >
              | > using A::Test();
              | >
              | > My question is, since the above can be done, is it possible to do the same
              | > for static_cast?
              |
              | Formal answer - no.
              |
              | But if you really want 'B*' to be convertible to 'A*' maybe you should
              | just make 'A' _public_ base class of 'B'. Although, come to think about
              | it, public inheritance usually implies a lot more than a mere pointer
              | convertibility. ..
              |
              | You can also use "brute force" to break through private inheritance by
              | using C-style cast
              |
              | B* b;
              | A* a;
              | ...
              | a = (A*) b;
              |
              | This will work. But this is as ugly as it ever gets.

              This might be even more ugly, but at least we can spot it :-):
              a = reinterpret_cas t<A*>( b );

              | Maybe more elegant solution would be to introduce a member function into
              | class 'B', which will return a pointer to its 'A' base
              |
              | class B : private A {
              | ...
              | public:
              | A* get_A() { return this; }
              | };
              |
              | Anyway, it would be useful if you could explain in more detail why
              | exactly you need this type of functionality.

              I prefer this, given the two options.

              Cheers.
              Chris Val


              Comment

              • Rob Williscroft

                #8
                Re: How do I expose a static_cast?

                Chris ( Val ) wrote in
                news:c29ps9$1re 7h0$1@ID-110726.news.uni-berlin.de:
                [color=blue]
                >
                > "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
                > news:104fj94jio 1esd2@news.supe rnews.com...
                >| news.ir.com.au wrote:
                >| > ...
                >| > Eg. If I was to add the method A::Test(), it is now possible to
                >| > access A::Test() inside class B by adding the following statement
                >| > to class B:
                >| >
                >| > using A::Test();
                >| >
                >| > My question is, since the above can be done, is it possible to do
                >| > the same for static_cast?
                >|
                >| Formal answer - no.
                >|
                >| But if you really want 'B*' to be convertible to 'A*' maybe you
                >| should just make 'A' _public_ base class of 'B'. Although, come to
                >| think about it, public inheritance usually implies a lot more than a
                >| mere pointer convertibility. ..
                >|
                >| You can also use "brute force" to break through private inheritance
                >| by using C-style cast
                >|
                >| B* b;
                >| A* a;
                >| ...
                >| a = (A*) b;
                >|
                >| This will work. But this is as ugly as it ever gets.
                >
                > This might be even more ugly, but at least we can spot it :-):
                > a = reinterpret_cas t<A*>( b );
                >[/color]

                This is one of the things that C-style casts do that can't be
                done by the other cast's. You're reinterpret_cas t<> will only
                work if the A subobject in B is at offset 0. The C-style cast
                will work regardless.

                Rob.
                --

                Comment

                • Chris \( Val \)

                  #9
                  Re: How do I expose a static_cast?


                  "Rob Williscroft" <rtw@freenet.RE MOVE.co.uk> wrote in message
                  news:Xns94A37D5 3157B3ukcoREMOV Efreenetrtw@195 .129.110.204...
                  | Chris ( Val ) wrote in
                  | news:c29ps9$1re 7h0$1@ID-110726.news.uni-berlin.de:
                  |
                  | >
                  | > "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
                  | > news:104fj94jio 1esd2@news.supe rnews.com...
                  | >| news.ir.com.au wrote:
                  | >| > ...
                  | >| > Eg. If I was to add the method A::Test(), it is now possible to
                  | >| > access A::Test() inside class B by adding the following statement
                  | >| > to class B:
                  | >| >
                  | >| > using A::Test();
                  | >| >
                  | >| > My question is, since the above can be done, is it possible to do
                  | >| > the same for static_cast?
                  | >|
                  | >| Formal answer - no.
                  | >|
                  | >| But if you really want 'B*' to be convertible to 'A*' maybe you
                  | >| should just make 'A' _public_ base class of 'B'. Although, come to
                  | >| think about it, public inheritance usually implies a lot more than a
                  | >| mere pointer convertibility. ..
                  | >|
                  | >| You can also use "brute force" to break through private inheritance
                  | >| by using C-style cast
                  | >|
                  | >| B* b;
                  | >| A* a;
                  | >| ...
                  | >| a = (A*) b;
                  | >|
                  | >| This will work. But this is as ugly as it ever gets.
                  | >
                  | > This might be even more ugly, but at least we can spot it :-):
                  | > a = reinterpret_cas t<A*>( b );
                  | >
                  |
                  | This is one of the things that C-style casts do that can't be
                  | done by the other cast's. You're reinterpret_cas t<> will only
                  | work if the A subobject in B is at offset 0. The C-style cast
                  | will work regardless.

                  Yes, you're right, in that the c-style cast is much more
                  powerful in this regard.

                  Thanks.
                  Chris Val


                  Comment

                  • Andrey Tarasevich

                    #10
                    Re: How do I expose a static_cast?

                    Chris ( Val ) wrote:[color=blue]
                    > |
                    > | You can also use "brute force" to break through private inheritance by
                    > | using C-style cast
                    > |
                    > | B* b;
                    > | A* a;
                    > | ...
                    > | a = (A*) b;
                    > |
                    > | This will work. But this is as ugly as it ever gets.
                    >
                    > This might be even more ugly, but at least we can spot it :-):
                    > a = reinterpret_cas t<A*>( b );
                    > ...[/color]

                    Yes, but this is not the same. The behavior of C-style cast in this case
                    is unambiguously defined by the language specification. And it performs
                    a correct derived-to-base conversion (ignoring any limitations imposed
                    by private inheritance).

                    On the contrary, the result of 'reinterpret_ca st' is implementation-defined.

                    --
                    Best regards,
                    Andrey Tarasevich

                    Comment

                    Working...