 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sun Sep 11, 2005 3:10 pm Post subject: vtable problem when using op.-> |
|
|
For a state machine like pattern I added the operator-> to my class.
Whenever the user of class C calls foo also operator-> will be called.
This works as long as foo isn't virtual. When foo is virtual it seems that
the vtable isn't set up correctly. Looking at the assembly code a call
like call [edx], with edx==0, is made. What's wrong here?
// operator.cpp
#include <iostream>
class C {
public:
C (int i = 0) : i_ (i) {}
virtual void foo ();
public:
C* operator-> ();
private:
int i_;
};
void C::foo () {
std::cout << "C::foo ()" << std::endl;
}
C* C::operator-> () {
std::cout << "C::operator-> ()" << std::endl;
return (this);
}
int main () {
class C c_; // not a pointer
c_->foo ();
return (0);
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Sun Sep 11, 2005 9:13 pm Post subject: Re: vtable problem when using op.-> |
|
|
[email]vl106 (AT) hotmail (DOT) com[/email] wrote:
| Quote: | For a state machine like pattern I added the operator-> to my class.
Whenever the user of class C calls foo also operator-> will be called.
This works as long as foo isn't virtual. When foo is virtual it seems that
the vtable isn't set up correctly. Looking at the assembly code a call
like call [edx], with edx==0, is made. What's wrong here?
|
First, what compiler are you using?
Second, what optimization levels were you using?
If you want to test correct code generation, you will need to make
sure that the proper pre-conditions are established; in particular,
you need to ensure that the compiler doesn't know enough to transform
your code into a purely static call.
For example, when I add the following modifications, I still get what
I expect (D::foo gets called), eventhough C: p->() returns a C*.
class D : public C {
public:
virtual void foo() {
std::cout << "D::foo" << std::endl;
}
};
int main () {
D c_; // not a pointer
c_->foo ();
return (0);
}
| Quote: | // operator.cpp
#include <iostream
class C {
public:
C (int i = 0) : i_ (i) {}
virtual void foo () { std::cout << "C::foo" << std::endl; }
public:
C* operator-> () { return this; }
private:
int i_;
};
[snip]
int main () {
class C c_; // not a pointer
c_->foo ();
return (0);
}
|
--
A. Kanawati
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|