由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++class指针转换
相关主题
pointer overflowWhy do I need to use "plain" pointer?
C++ Q05: pointer to constant variableis smart_ptr really that good?
请教 C++的一个困惑 (operator delete)C++ Q93 - Q95 (转载)
[合集] can one type cast reference in C++int F::*x = &F::x是什么意思?
What does int & mean? C/C++ questionc++ 里用到pointer 的地方我们尽可能用smart pointer吗?
C++ Q87: What is wrong with this swap function? (转载)不如各位高手挑个专题讲讲C++11吧
这个function pointer最后的那个int是什么意思?Go adopts JavaScript’s idea of semicolon insertion
what's wrong with this C++ code?shared_ptr and dynamic_pointer_cast
相关话题的讨论汇总
话题: c++话题: void话题: bird话题: fly话题: cast
进入Programming版参与讨论
1 (共1页)
g***a
发帖数: 114
1
thinking in C++ 的一道作业题,关于class的指针转换,想说明C中存在此类"hole",
而C++可以规避。可是自己编程发现程序结果与solution刚好相反,望指点。
原题如下
Create a class called bird that can fly( ) and a class rock that can’t.
Create a rock object, take its address, and assign that to a void*. Now take
the void*, assign it to a bird* (you’ll have to use a cast), and call fly(
) through that pointer. Is it clear why C’s permission to openly assign
via a void* (without a cast) is a “hole” in the language, which couldn’t
be propagated into C++?
solution:
This is the whole point of C++’s type system! You shouldn’t be able to
accidentally misuse an object through pointer gyrations. If this were
allowed, you could indirectly call any function at all for any object at all
without the compiler complaining. Not a smart way to program.
=============================================================
我的代码如下
#include "stdio.h"
class bird{
public:
void fly(){printf("I can fly\n");}
};
class rock{
};
int main()
{
rock rk;
void* pt = &rk;
//bird* bp = static_cast(pt);
bird* bp = (bird*)(pt);
bp->fly();
return 0;
}
====================================================
我的问题:我发现上面的代码在C++中可以通过并调用fly(),而在C中编译则出现问题
。难道这个现在说明是C++中存在“hole",而不是C中,不太明白solution的意思,请
指点。谢谢!
f*******n
发帖数: 12623
2
但是你需要cast。在C里,不用cast
b***i
发帖数: 3043
3
maybe teacher talks about virtual function

take
fly(
t

【在 g***a 的大作中提到】
: thinking in C++ 的一道作业题,关于class的指针转换,想说明C中存在此类"hole",
: 而C++可以规避。可是自己编程发现程序结果与solution刚好相反,望指点。
: 原题如下
: Create a class called bird that can fly( ) and a class rock that can’t.
: Create a rock object, take its address, and assign that to a void*. Now take
: the void*, assign it to a bird* (you’ll have to use a cast), and call fly(
: ) through that pointer. Is it clear why C’s permission to openly assign
: via a void* (without a cast) is a “hole” in the language, which couldn’t
: be propagated into C++?
: solution:

h**********c
发帖数: 4120
4
The difference is c doesn't have class, you have to use struct, please
verify. Or page back carefully with the book you mentioned. I did every ex
of both the two TIC volumes. Almost no discrepancy. They are real master
pieces.
For example, I use Visual c++ 2010. Copy paste your code to main.cpp. Your
code works.
If you change the file name to main.c, there will be a bunch of errors.
g***a
发帖数: 114
5

是啊,这正是我的问题所在,按照题目和答案的意思,C++为防止bug,在这类转换很严
格,编译无法通过,而C存在"hole",所以编译可以通过。可是事实正好相反,我的
code在C++里能通过,可是C里却不能通过。这难道是说明C++里存在"hole"而C却没有?

【在 h**********c 的大作中提到】
: The difference is c doesn't have class, you have to use struct, please
: verify. Or page back carefully with the book you mentioned. I did every ex
: of both the two TIC volumes. Almost no discrepancy. They are real master
: pieces.
: For example, I use Visual c++ 2010. Copy paste your code to main.cpp. Your
: code works.
: If you change the file name to main.c, there will be a bunch of errors.

h**********c
发帖数: 4120
6
Ok let's just talk about the hole.
the hole can mean in struct, everything is public. first.
second, I didn't test, can you re-write your code with struct?
then try to do sth
void * pt = new bird() etc.
"to openly assign
via a void* (without a cast) is a “hole” in the language "
That is too much detailed. I think in the book, at least, the problem with c
is mentioned -- struct has no protected, private, everything is public.
So my understanding, in C, you have the interface of object, you can call all the functions. against C++, only public is visible.
Good luck.
f*******n
发帖数: 12623
7
In C, you can assign something of type void * to any pointer type without
cast.
For example,
int *foo = malloc(42);
malloc returns void *, but it can be assigned to any pointer type without
cast.
In C++, you cannot assign void * to another pointer type without cast.
That is the difference.
k******I
发帖数: 238
8
看看转换以后bp是否是null, 就算是null->fly()也会工作,因为fly不是虚函数,也没
有用data member
i*********n
发帖数: 58
9
In C++, assign a void* to a pointer T* is illegal without casting, however
in C it's legal. When you do casting, you dishonor the type system and
should know what you are doing. In your example, with the casting, your
program may not crash, but of course will give wrong results. So casting is
something you need to be really careful.
1 (共1页)
进入Programming版参与讨论
相关主题
shared_ptr and dynamic_pointer_castWhat does int & mean? C/C++ question
pointer to functionC++ Q87: What is wrong with this swap function? (转载)
听说pointer to member function只支持最多128个virtual functi这个function pointer最后的那个int是什么意思?
大侠给解释下c++为何会允许这种polymorphism?what's wrong with this C++ code?
pointer overflowWhy do I need to use "plain" pointer?
C++ Q05: pointer to constant variableis smart_ptr really that good?
请教 C++的一个困惑 (operator delete)C++ Q93 - Q95 (转载)
[合集] can one type cast reference in C++int F::*x = &F::x是什么意思?
相关话题的讨论汇总
话题: c++话题: void话题: bird话题: fly话题: cast