473,785 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
9 4174
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


Jul 22 '05 #2
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

Jul 22 '05 #3
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

Jul 22 '05 #4
In article <nL************ *****@news.optu s.net.au>,
"news.ir.com.au " <da*******@yaho o.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;
}

Jul 22 '05 #5
> 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!
Jul 22 '05 #6

"Andrey Tarasevich" <an************ **@hotmail.com> wrote in message
news:10******** *****@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
Jul 22 '05 #7
Chris ( Val ) wrote in
news:c2******** *****@ID-110726.news.uni-berlin.de:

"Andrey Tarasevich" <an************ **@hotmail.com> wrote in message
news:10******** *****@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.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #8

"Rob Williscroft" <rt*@freenet.RE MOVE.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.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
Jul 22 '05 #9
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_cas t<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_ca st' is implementation-defined.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6366
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;
11
5155
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 one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an online site that can explain it to me?
26
2559
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 favor using a static_cast. Is there any technical reason to favor the C-style over a static_cast? http://doc.coin3d.org/Coin/index.html void foo(SoNode * node) { if (node->getTypeId() == SoFile::getClassTypeId()) {
3
1930
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
2
1961
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.. template <class T> class Node{ protected: T e; Node* left;
19
3761
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 other.
24
4767
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);
5
3913
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 static_cast<>? In other words, is there any real difference between statements like (with non-pointer types): double a = 3.4; int b = (int)a; // <--- this
3
367
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; // Compilation error , OK i= static_cast<int *>(d); // Compilation error. OK
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.