How do I expose a static_cast?

N

news.ir.com.au

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
 
R

Rolf Magnus

news.ir.com.au said:
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
 
N

news.ir.com.au

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::eek:perator static_cast;
using A::eek:perator static_cast<>;
..
..
..

Please let me know if you still do not understand.

Thanks,

David


Rolf Magnus said:
news.ir.com.au said:
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
 
A

Andrey Tarasevich

news.ir.com.au said:
...
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?)
 
C

Clark Cox

news.ir.com.au said:
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;
}
 
T

Tom Plunket

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!
 
C

Chris \( Val \)

| 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
 
R

Rob Williscroft

Chris ( Val ) wrote in
| 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.
 
C

Chris \( Val \)

| Chris ( Val ) wrote in
| |
| >
| > | >| 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
 
A

Andrey Tarasevich

Chris said:
|
| 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top