Ruby - C++ extension

  • Thread starter Elias Athanasopoulos
  • Start date
E

Elias Athanasopoulos

Hello!

I am trying to create a new Ruby extension, which eventually will
give to Ruby the functionality of a C++ project.

So far, the extension is going fine. However I have two issues:

(1) C++ Type Casting
(2) Multiple Inheritance

Is there a generic approach to solve the above?

As far as C++ Type Casting is concerned I am thinking of creating
to_xxx methods. I.e.

foo = (Foo *) Bar->dump();

will be in Ruby:

foo = Bar.dump.as_Foo

As far as Multiple Inheritence is concerned, I think the only way
to go through is to define modules and include them in other modules.
Howver the C++ project I'm trying to create Ruby Bindings for, has a very
complex inheritence tree. Is there any other workaround?

Regards,
 
P

Paul Brannan

As far as C++ Type Casting is concerned I am thinking of creating
to_xxx methods. I.e.

foo = (Foo *) Bar->dump();

This is not C++ type casting; this is a C-style cast. In C++, you
generally want to avoid C-style casts because C-style casts are far too
powerful (they let you lie to the compiler far too easily). Please
consider using static_cast said:
will be in Ruby:

foo = Bar.dump.as_Foo

I'm not sure this really applies well to Ruby. Because Ruby is
dynamically typed, there's no reason why dump() can't just return an
object of the appropriate type (e.g. return a Foo if you really have a
Foo or return a Bar if you really have a Bar).
As far as Multiple Inheritence is concerned, I think the only way
to go through is to define modules and include them in other modules.
Howver the C++ project I'm trying to create Ruby Bindings for, has a very
complex inheritence tree. Is there any other workaround?

Yes, modules are the way to go. See [ruby-talk:66580] and my suggestion
in [ruby-talk:66624].

Paul
 
E

Elias Athanasopoulos

This is not C++ type casting; this is a C-style cast. In C++, you
generally want to avoid C-style casts because C-style casts are far too
powerful (they let you lie to the compiler far too easily). Please
consider using static_cast<> and dynamic_cast<> whenever possible.

Thanks for all the replies in my question. I was to busy to answer.
My apologies for the delay.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top