t**e 发帖数: 208 | 1 每个类都有一个virtual table, 这个table是存在code segment 还是data segment?
谢谢~~ |
a**********s 发帖数: 588 | 2 I think it doesn't matter but shouldn't be in the code segment... I guess
it's in the constant portion of data segment. |
Q******e 发帖数: 85 | 3 More effective C++ 说了,通常virtual table is generated in the object file
containing the definition of the first non-inline non-pure virtual function
in that class. 我认为virtual table应该在代码段。 |
a**********s 发帖数: 588 | 4 It was talking about something else.
Consider if it were kept in the code segment, how could the compiler generate code to do dynamic binding - it needs to calculate the entrance address to know where to jump to.
function
【在 Q******e 的大作中提到】 : More effective C++ 说了,通常virtual table is generated in the object file : containing the definition of the first non-inline non-pure virtual function : in that class. 我认为virtual table应该在代码段。
|
Q******e 发帖数: 85 | 5 Dynamic binding is decided by the actual object type. suppose Base* ptr =
new Derived. ptr actually point to a Derived object and compiler knows it.
Through ptr,we can get virtual table pointer vptr (the code segment address)
of Derived object. When running, the virtual function can be found through
vptr with virtual function index (offset). In that way, we have dynamic
binding. |
a**********s 发帖数: 588 | 6 Just to write a simple program
#include
class Base
{
public:
virtual int drive() { return 0; }
};
class Derived : public Base
{
public:
int drive() { return 1; }
};
int main()
{
Base* p = new Derived();
std::cout << p->drive() << std::endl;
delete p;
return 0;
} |
Q******e 发帖数: 85 | 7 So? what is the purpose of your code? |
a**********s 发帖数: 588 | 8 And the generated assembly, I am showing the const data segment only: |
a**********s 发帖数: 588 | 9 hmm, I found it's bely hard to convince people :( |
Q******e 发帖数: 85 | 10 Actually, using the dynamic binding method, we can put the code inside data
segment. But why we want to break the traditional routine? data is data,
code is code. |
|
|
Q******e 发帖数: 85 | 11 using g++ -S, you can find immediate code with Assembly language. |
a**********s 发帖数: 588 | 12 That's intermediate assembly. No clear concept of code/text segments.
However, when you look carefully, you still see virtual tables are
declared as data there.
【在 Q******e 的大作中提到】 : using g++ -S, you can find immediate code with Assembly language.
|
a**********s 发帖数: 588 | 13 What are you talking about.. You think virtual table is code??? How could
dynamic binding permit you to do this??
data
【在 Q******e 的大作中提到】 : Actually, using the dynamic binding method, we can put the code inside data : segment. But why we want to break the traditional routine? data is data, : code is code.
|
M*****a 发帖数: 2054 | 14 I agree with u
in fact, class is an object, virtual table should be data
but it maybe const data
【在 a**********s 的大作中提到】 : What are you talking about.. You think virtual table is code??? How could : dynamic binding permit you to do this?? : : data
|
Q******e 发帖数: 85 | 15 My fault, I always think the topic is about where to put the virtual
function. I should watch carefully about the topic. Sorry. |
s*****e 发帖数: 60 | 16 I think it depends on the implementation of compiler. |