[gmpi] Re: C++ detail question

It's a pretty dangerous operation anyway... if CFoo::method1() accesses any members of CFoo that aren't in Foo, you'll crash straightaway.



Ron Kuper wrote:

In VC, the compiler barfs on the cfoo1 assignment.  No surprise since
it's a member of CFoo, therefore not visible at global scope.

The call to cfoo->method2() crashes.

-----Original Message-----
From: gmpi-bounce@xxxxxxxxxxxxx [mailto:gmpi-bounce@xxxxxxxxxxxxx] On
Behalf Of Tim Hockin
Sent: Wednesday, March 02, 2005 4:23 PM
To: gmpi@xxxxxxxxxxxxx
Subject: [gmpi] Re: C++ detail question

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


---------------------------------------------------------------------- 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







--
=========================================================
Angus F. Hewlett, Managing Director (CEO)
FXpansion Audio UK Ltd - http://www.fxpansion.com
Registered in the UK - #4455834 - VAT: GB 798 7782 33
=========================================================



---------------------------------------------------------------------- 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

Other related posts: