我如何公开static_cast? [英] How do I expose a static_cast?
问题描述
在下面的代码中我得到编译器错误:
错误C2243:''static_cast'':转换自''B级''到''A级'*'
存在,但是不可访问
我明白为什么我会收到此错误并且通常可以通过插入使用A :: ...来解决这种情况
B类中的声明,但是,由于这个
是静态演员,语法是什么?
--------
A级
{
};
B级:私人A
{
};
int main(int argc,char * argv [])
{
B * b;
A * a;
a = static_cast< A *>(b);
返回0;
}
谢谢,
David
news.ir.com.au写道:
在下面的代码中我收到编译错误:
错误C2243:''static_cast'':从''B类'''转换为''A类
*''存在,但无法访问
我理解为什么我会收到这个错误,并且通常可以通过插入using A :: ...来解决这个问题。 B类中的语句,然而,由于这是静态演员,语法是什么?
什么的语法?
B对象的A部分是私有的,即外部无法访问
世界。因此,您无法将B指针转换为A指针。我是
不确定你的意思是我明白为什么会收到这个错误。
无论如何,我现在不明白你的问题是。
A级
{
};
B级:私人A
{
}; >
int main(int argc,char * argv [])
{
B * b;
A * a;
a = static_cast< A *>(b);
返回0;
}
谢谢,
David
>
Rolf,
感谢您的回复。
我的意思是,我可以我知道由于A类是B类无法访问的,因此我得到了这个错误。但是可以明确地允许成员使用使用来访问成员
。关键字。
例如。如果我要添加方法A :: Test(),现在可以通过在B类中添加以下语句来访问B类中的
A :: Test():
使用A :: Test();
我的问题是,既然上面可以做到,是否可以这样做
for static_cast?
我尝试过使用A :: static_cast明显的:
;
和其他变体如:
使用A :: operator static_cast;
使用A :: operator static_cast<> ;
..
..
..
如果你还是请告诉我不明白。
谢谢,
David
" Rolf Magnus" < RA ****** @ t-online.de>在消息中写道
news:c2 ************* @ news.t-online.com ...news.ir. com.au写道:
在下面的代码中我得到编译错误:
错误C2243:'' static_cast'':从''B类*''转换为''A类
*''存在,但是无法访问
我理解为什么我会收到此错误并且通常可以绕过通过插入使用A :: ...来实现这种情况。 B类中的语句,然而,由于这是静态演员,语法是什么?
什么语法?
B的A部分对象是私有的,即外部世界无法访问。因此,您无法将B指针转换为A指针。我不确定你的意思是我明白为什么会收到这个错误。
无论如何,我不明白你现在的问题是什么。
A级
{
};
B类:私人A
{
};
> int main(int argc,char * argv [])
{* B * b;
A * a;
a = static_cast< A *> (b);
返回0;
}
谢谢,
大卫
news.ir.com.au写道:...
例如。如果我要添加方法A :: Test(),现在可以通过在B类中添加以下语句来访问B类中的A :: Test():
使用A :: Test();
我的问题是,既然可以做到,可以对static_cast做同样的事情吗?
正式答案 - 没有。
但如果你真的希望''B *''可以兑换成''A *'',那么你应该
只需要'''''_'公共基类''B''。虽然,来考虑一下它,公共继承通常意味着不仅仅是一个指针
兑换......
你也可以使用蛮力。使用C风格的演员来突破私人继承
B * b;
A * a;
...
a =(A *)b;
这将有效。但这有点像它一样难看。
也许更优雅的解决方案是将成员函数引入
class''B'',将返回指向其A基地的指针
B级:私人A {
...
public :
A * get_A(){return this; }
};
无论如何,如果你能更详细地解释为什么
你需要这种类型它会很有用功能。
(请你停止发帖吗?)
-
祝你好运,
Andrey Tarasevich
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
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
这篇关于我如何公开static_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!