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 9 4141
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
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" <ra******@t-online.de> wrote in message
news:c2*************@news.t-online.com... 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
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.
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
In article <nL*****************@news.optus.net.au>,
"news.ir.com.au" <da*******@yahoo.com> wrote: 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;
}
> 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!
"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:10*************@news.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 );
| 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
Chris ( Val ) wrote in
news:c2*************@ID-110726.news.uni-berlin.de: "Andrey Tarasevich" <an**************@hotmail.com> wrote in message news:10*************@news.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/
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.204...
| Chris ( Val ) wrote in
| news:c2*************@ID-110726.news.uni-berlin.de:
|
| >
| > "Andrey Tarasevich" <an**************@hotmail.com> wrote in message
| > news:10*************@news.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
Chris ( Val ) wrote: | | 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gary Labowitz |
last post by:
Am I doing this correctly? It is a sample program for my class.
#include <iostream>
using namespace std;
int main( )
{
int x=3, y=4;
|
by: Scott Brady Drummonds |
last post by:
Hi, everyone,
I've checked a couple of on-line resources and am unable to determine how
reinterpret_cast<> is different from static_cast<>. They both seem to
perform a compile-time casting of...
|
by: Steven T. Hatton |
last post by:
The code shown below is an example from the Coin3D documentation. I believe
the use of the C-style cast is safe under the circumstances, but from what
I've been exposed to (TC++PL(SE)), I would...
|
by: shrishjain |
last post by:
Hi All,
Do people frequently use static_cast, const_cast etc in industry?.. I
only saw them in books, and never in real code..
Shrish
|
by: Amit |
last post by:
Greetings.
I am having some problem while using a cast operation(static_cast and/or
dynamic_cast) between base and derived objects when passing to functions.
what I have is something like this..
...
|
by: PengYu.UT |
last post by:
I see some code use static_cast<some_pointer_type>(0) instead of NULL
to describe null pointer. I'm wondering what is the pros and cons of
each way. Is there any reason why we should one verses the...
|
by: Rahul |
last post by:
Hi,
I have a
class A : public B {...member functions......data members};
and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);
|
by: jason.cipriani |
last post by:
There have been some recent threads about casting pointers to and from
void* that have me rethinking some of my usual practices. I have a
couple of questions.
1. What is the purpose of C++'s...
|
by: Rahul |
last post by:
Hi,
Everywhere I read that static_cast<only work fine for the conversion
which are implicitly allowed by the compiler
hence the following does not work
int *i;
double *d;
d = i; ...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |