cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Calling a non const C++ class member function from Rust

Open vinayakadj opened this issue 2 years ago • 2 comments

I am trying to call a non const C++ member function from rust but I am getting an error. Is it possible to call non const member function from Rust?

class MyClass { // The class public: // Access specifier int myNum; // Attribute (int variable) string myString; // Attribute (string variable) void function(weak_ptr<MyClass>) const; void function2() const; // I am able to call void function3(); // Not able to call this function ~MyClass(); };

Rust

#[cxx::bridge] mod ffi { unsafe extern "C++" { include!("demo/src/sample.h"); type MyClass; fn new_MyClass() -> UniquePtr<MyClass>; fn function(&self,a:WeakPtr<MyClass>) ; fn function2(&self); fn function3( &self ); } }

fn main() { println!("Hello, world!"); let mut ob = ffi::new_MyClass(); ob.function2(); ob.as_mut().function3();

}

vinayakadj avatar Feb 01 '23 07:02 vinayakadj

Please put the code between ```cpp and ``` as well as ```rust and ```!

Enyium avatar Feb 01 '23 21:02 Enyium

See https://github.com/dtolnay/cxx/issues/1083#issuecomment-1222542362

nneesshh avatar Jul 20 '23 05:07 nneesshh