I'm new to cplusplus and I have no idea how to do implicit type conversion between structs.
I want to do the following:
A a = new __A();
Object o = a;
I know this requires operator overloading (I think?) and yet, trying to implement operator overloading has been futile. I've used the examples on this article: http://www.cplusplus.com/doc/tutorial/typecasting/ and yet i cant get anything to work. Any help would be very appreciative. Here's the layouts of my structs.
typedef __Object* Object;
typedef __Class* Class;
typedef __String* String;
typedef __A* A;
typedef __Test002* Test002;
struct __Object {
__Object_VT* __vptr;
// The constructor.
__Object();
// The methods implemented by java.lang.Object.
static int32_t hashCode(Object);
static bool equals(Object, Object);
static Class getClass(Object);
static String toString(Object);
// The function returning the class object representing
// java.lang.Object.
static Class __class();
// The vtable for java.lang.Object.
static __Object_VT __vtable;
};
struct __A {
__A_VT* __vptr;
__A();
static __String* toString(A);
static int32_t hashCode(Object);
static bool equals(Object, Object);
static Class getClass(Object);
static Class __class();
static __A_VT __vtable;
};
A few things about the code you posted and your objectives
So maybe something like the following
#include <string>
class Object
{
/* ... */
public:
virtual ~Object() = default; // necessary to avoid slicing
virtual std::string toString() const = 0;
/* ... */
};
/* virtual inheritance necessary here if you plan on
deriving from multiple classes that derive from Object;
otherwise remove */
class A : public virtual Object
{
/* ... */
public:
// may or may not be needed, viz., above
// virtual ~A() = default;
std::string toString() const override { return std::string{ "A" }; }
/* ... */
};
#include <iostream>
#include <memory>
int main()
{
std::unique_ptr<Object> o{ std::make_unique( A ) };
std::cout << o->toString() << '\n'; // prints "A"
}
There's a lot in there so ask questions if you're confused by anything I wrote (although it's likely to get off-topic quickly).