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 | | b***i 发帖数: 3043 | 3 maybe teacher talks about virtual function
take
fly(
t
【在 g***a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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. |
|