 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
news.ir.com.au Guest
|
Posted: Thu Mar 04, 2004 11:52 pm Post subject: 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
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Mar 04, 2004 11:56 pm Post subject: Re: How do I expose a static_cast? |
|
|
news.ir.com.au wrote:
| Quote: | 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?
|
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.
| Quote: | 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
|
|
|
| Back to top |
|
 |
news.ir.com.au Guest
|
Posted: Fri Mar 05, 2004 12:22 am Post subject: 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 (AT) t-online (DOT) de> wrote
| Quote: | news.ir.com.au wrote:
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?
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.
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
|
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Fri Mar 05, 2004 12:43 am Post subject: Re: How do I expose a static_cast? |
|
|
news.ir.com.au wrote:
| Quote: | ...
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.
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
|
|
| Back to top |
|
 |
Clark Cox Guest
|
Posted: Fri Mar 05, 2004 1:07 am Post subject: Re: How do I expose a static_cast? |
|
|
In article <nLP1c.225$IH5.11956 (AT) news (DOT) optus.net.au>,
"news.ir.com.au" <davidp_au (AT) yahoo (DOT) com> wrote:
| Quote: | 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?
|
No. The closest you could do would be to add some function to B, like
the following:
A *B::GetAPointer()
{
return this;
}
|
|
| Back to top |
|
 |
Tom Plunket Guest
|
Posted: Fri Mar 05, 2004 1:21 am Post subject: Re: How do I expose a static_cast? |
|
|
| Quote: | 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.
|
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!
|
|
| Back to top |
|
 |
Chris ( Val ) Guest
|
Posted: Fri Mar 05, 2004 11:55 am Post subject: Re: How do I expose a static_cast? |
|
|
"Andrey Tarasevich" <andreytarasevich (AT) hotmail (DOT) com> wrote
| Quote: | 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_cast<A*>( b );
| Quote: | 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
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Fri Mar 05, 2004 12:19 pm Post subject: Re: How do I expose a static_cast? |
|
|
Chris ( Val ) wrote in
news:c29ps9$1re7h0$1 (AT) ID-110726 (DOT) news.uni-berlin.de:
| Quote: |
"Andrey Tarasevich" <andreytarasevich (AT) hotmail (DOT) com> wrote in message
news:104fj94jio1esd2 (AT) news (DOT) supernews.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_cast<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_cast<> will only
work if the A subobject in B is at offset 0. The C-style cast
will work regardless.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
|
|
| Back to top |
|
 |
Chris ( Val ) Guest
|
Posted: Fri Mar 05, 2004 12:29 pm Post subject: Re: How do I expose a static_cast? |
|
|
"Rob Williscroft" <rtw (AT) freenet (DOT) REMOVE.co.uk> wrote
| Quote: | Chris ( Val ) wrote in
news:c29ps9$1re7h0$1 (AT) ID-110726 (DOT) news.uni-berlin.de:
"Andrey Tarasevich" <andreytarasevich (AT) hotmail (DOT) com> wrote in message
news:104fj94jio1esd2 (AT) news (DOT) supernews.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_cast<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_cast<> 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
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Fri Mar 05, 2004 9:43 pm Post subject: Re: How do I expose a static_cast? |
|
|
Chris ( Val ) wrote:
| Quote: | |
| 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_cast<A*>( b );
...
|
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_cast' is implementation-defined.
--
Best regards,
Andrey Tarasevich
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|