[gmpi] Re: C++ detail question
- From: Tim Hockin <thockin@xxxxxxxxxx>
- To: gmpi@xxxxxxxxxxxxx
- Date: Wed, 2 Mar 2005 13:23:29 -0800
On Wed, Mar 02, 2005 at 04:00:15PM -0500, Paul Davis wrote:
> >> its guaranteed to be C compatible, which leads to the data elements
> >> coming first.
> >
> >OK, so second question. If I cast a "struct Foo *" to a "class CFoo *",
> >what happens? Can I use non-virtual methods on it? Can I use virtual
> >methods on it?
> >
> >Obviously, I could not access any data members that are part of CFoo but
> >not Foo, right?
>
> not sure if i understand the question completely. which one is
> declared in what language?
/* defined in C */
struct Foo
{
int foo1;
int foo2;
};
/* defined in C++ */
class CFoo : public Foo
{
public:
int method1(void) { return 0; }
virtual int method2(void) { return 0; }
int cfoo1;
};
void func(struct Foo *foo)
{
CFoo* cfoo = static_cast<CFoo*>(foo);
cfoo->method1(); /* safe? */
cfoo->method2(); /* not safe */
cfoo1 = 12345; /* not safe */
}
My assumption is that it is safe to call CFoo::method1 because the
compiler will do static binding. Now, if method1 does something unsafe,
that's a different story. Assuming method1 is safe internally, is this
safe to do?
It seems pretty clear that CFoo::method2 is illegal, as the CFoo object
does not have a valid vtable. Accessing the cfoo1 member is likewise
bad because it doesn't exist.
The question is whether it is safe to access method1?
Specifically, there is a C struct that I want to wrap in C++, including
a couple of operator methods (needed to be put into a map). We could
create a new CFoo and initialize it with the Foo, but it seems like
there will be a lot of construct/destruct cycles wasted there...
----------------------------------------------------------------------
Generalized Music Plugin Interface (GMPI) public discussion list
Participation in this list is contingent upon your abiding by the
following rules: Please stay on topic. You are responsible for your own
words. Please respect your fellow subscribers. Please do not
redistribute anyone else's words without their permission.
Archive: http://www.freelists.org/archives/gmpi
Email gmpi-request@xxxxxxxxxxxxx w/ subject "unsubscribe" to unsubscribe
- References:
- [gmpi] Re: C++ detail question
- From: Tim Hockin
- [gmpi] Re: C++ detail question
- From: Paul Davis
Other related posts:
- » [gmpi] C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- » [gmpi] Re: C++ detail question
- [gmpi] Re: C++ detail question
- From: Tim Hockin
- [gmpi] Re: C++ detail question
- From: Paul Davis