C++ plugin fundamentals, object props and methods
-
On 18/05/2013 at 08:08, xxxxxxxx wrote:
User Information:
Cinema 4D Version: R14 Stud
Platform: Windows ;
Language(s) : C++ ;---------
I am rather new to C++, but not to programming, 15 years of assembler, Pascal and C#.
When trying to write plugins, there are a few fundamentals I have to learn and understand.
Take this:BaseObject* op = bc->GetObjectLink(MYPLUGIN_LINK_SOURCE, doc);
I would now expect to have access to all BaseObject's methods and properties, and it looks like that too. So so far, so good. But the BaseObject can be a Cube, a Spline or a Joint.
So I write this:CAJointObject* someJoint_a = (CAJointObject* )op; CAJointObject* someJoint_b = static_cast<CAJointObject*>(op);
- When the linked object is a Joint, I would expect to have access to the Joint specific properties (and methods). But I don't see them when invoking Intellisense.
- But even more confusing - if the linked object is not a Joint , when doing this in C# or Pascal, my computer will snare, spit, catch fire and bombard me with nasty error messages "invalid typecast" and the like. But not here. In fact, the watch window claims the Type is CAJointObject, which it definitely not is, because I have linked in a Cube.
My challenge now, is to be able to work the way I am used to in object oriented programming, that is having objects inheriting other objects, and having access to their properties and methods in a "normal" object oriented way.
If someone could lead me in the right direction, it is very much appreciated!-Ingvar
-
On 18/05/2013 at 13:16, xxxxxxxx wrote:
Howdy,
Try this:
BaseObject* op = bc->GetObjectLink(MYPLUGIN_LINK_SOURCE, doc); if(op && op->GetType() == Ojoint) { CAJointObject* someJoint_b = static_cast<CAJointObject*>(op); if(someJoint_b) { //do something with the joint } }
Adios,
Cactus Dan -
On 18/05/2013 at 13:29, xxxxxxxx wrote:
Hi, I see you AND them, I will try that. Apart from this, the C++ way of doing things is very different from what I am used to, it seems. Again - I find it hard not to just use objects, their methods and parameters and use inheritance the "normal" way. But I will learn..
-Ingvar -
On 20/05/2013 at 05:03, xxxxxxxx wrote:
Originally posted by xxxxxxxx
Apart from this, the C++ way of doing things is very different from what I am used to, it seems.
If you use a static cast this does not include a runtime type-check, instead use dynamic_cast and you'll get the type-check (if it's not of the type to be casted to or if it's not possible to cast to that type you'll get a null pointer).
-
On 20/05/2013 at 05:20, xxxxxxxx wrote:
Yes, I am used to that in other languages.
But I am also used to, when using a static cast, that somethings goes wrong when the cast is invalid. And here it seems like it is all fine.-Ingvar
-
On 20/05/2013 at 05:32, xxxxxxxx wrote:
Originally posted by xxxxxxxx
Yes, I am used to that in other languages.
But I am also used to, when using a static cast, that somethings goes wrong when the cast is invalid. And here it seems like it is all fine.Yes, but that's what static_cast does.
It is not type-safe but it also avoids the overhead of the type-check so actually it gives you high-level control over performance-safety. This is an advantage! Don't be lazy...(knowing C# myself quite well I know that it is more convenient), never. You will pay with hair and nails if you are...believe me.
-
On 20/05/2013 at 08:10, xxxxxxxx wrote:
I use static_cast a lot as a habit. So wanted to try this out.
But I get an error with dynamic_cast when using it like this.//CAJointObject *myJoint = static_cast<CAJointObject*>(obj); //Works fine CAJointObject *myJoint = dynamic_cast<CAJointObject*>(obj); //Error: BaseObject is not a polymorphic type
Is there a different way to write it. Or is casting from BaseObject to another type not possible with dynamic_cast?
-ScottA
-
On 20/05/2013 at 08:32, xxxxxxxx wrote:
No it's correctly written. And yep, base-to-derived casts with dynamic_cast is only possible when the base class is polymorphic (which the BaseObject is not according to the compiler error message...didn't know myself so apparently it cannot be used in this situation). You also need RTTI enabled for it to work.
-
On 20/05/2013 at 09:53, xxxxxxxx wrote:
I use the C-style casts a lot. static_cast() is much more to write, not that I'm lazy but I prefer to keep lines
short and avoid line-wrapping. -
On 20/05/2013 at 15:29, xxxxxxxx wrote:
Well, I prefer to use the C++ style casts because they have certain advantages. First of all the casts are checked by the compiler. You can also easily search for them and the programmer's intention is expressed more clearly.