Implicit type conversion with structs

186 views Asked by At

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;
};
2

There are 2 answers

0
bpw1621 On BEST ANSWER

A few things about the code you posted and your objectives

  • C++ handles virtual method dispatch for you through inheritance so there is no need to hand-jam virtual tables into your classes
  • As others have mentioned you are using invalid/reserved identifiers
  • If you are creating a singly rooted inheritance hierarchy (like in Java or to add something like reflection support) then you probably want to refactor all of the static methods in the Object class to be virtual or pure virtual member functions, have class A publically derive from class Object and override those methods
  • your use-case at the top implies what you really want is to be able to have a pointer to an object of class A use class Object's interface: this is again virtual dispatch enabled by public inheritance in C++

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

0
sfjac On

For this to work, __A would need to inherit from __Object as that's the only way you can assign a pointer to the former to one of the latter.