Thread: Troublesome operator overload

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    24

    Troublesome operator overload

    I'm currently attempting to simplify the use of my 'data-type' (var_var) by overloading operators in relation to it. First, an example of its usage:

    Code:
    class Test { // The test class.
    public: // Only public members for simplicity.
      var_var *HP;
      var_var *MP;
    // functions omitted for clarity.
    }
    The var_var pointers point to dynamically allocated derived classes of var_var (which, by itself, is of no importance) which are able to hold values of differing sizes. The size of the value held/assigned etc is taken into account and the pointer redirected to a larger/smaller type as needed. To make it simpler to interface with, I have been overloading operators to make syntax such as

    Code:
    int x = (ob.HP + ob.MP)
    useable, as opposed to

    Code:
    int x = (ob.HP->getval() + ob.MP->getval())
    Unfortunately, I am unable to work out how to implement the assignment operator in the following context.

    Code:
    Test ob; // Assume default initialisation.
    ob.HP = 300; // Dereference operator intentionally omitted.
    While I know the cause of the error (the assignment operator is overloaded for a var_var and int assignment, while this is a var_var * and int assignment) I do not know how to remedy this while keeping the usage syntax this simple. Because the assignment operator has to be a member of the class, I am unable to make the left operand anything other than the this pointer.

    (Yes, I know it may seem strange that I don't want to use the dereference operator, but I'm trying to make this have a similar interface to built-in integers, without using pointing-related operators).

    Also, I would prefer answers other than 'change your compiler' (I'm using Borland's free 5.5 version).

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Operator overloads aren't considered when you have a pointer to a class. The overloads would have to be for the pointer, not the class, because that's the type you're handling.

    Either change the objects so that you can put them directly into Test (not dynamically allocated) or create another class that acts as a smart pointer and contains the overloads.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    Operator overloads aren't considered when you have a pointer to a class. The overloads would have to be for the pointer, not the class, because that's the type you're handling.
    I know, what I'm trying to do is overload the operators to work with the pointers, but due to the assignment operator being a member function, I don't know how to have the pointer as the left operand. If it is possible to define a 'var_var *' type other than the default, I have been unable to find anything about it and would appreciate an example.

    Either change the objects so that you can put them directly into Test (not dynamically allocated) or create another class that acts as a smart pointer and contains the overloads.
    I'm afraid non-dynam-alloc objects are not possible. The idea behind the var_var is that no actual objects of its type are created, rather that pointers of its type point to objects of derived classes (such as uint8, sint16 etc) which are capable of holding values of different sizes and, consequently, happen to have different sizes (for example, a uint16 is 3 bytes, 2 to hold a 16-bit value and 1 as a more space-efficient way than virtual pointers to tell object type at runtime).

    While I do plan to create another class to act as a 'smart pointer', I would prefer to implement this functionality first to narrow down the region I have to look through for errors while testing it.

    (If anyone looking at this thinks that the var_var is non-sensical for space/processing efficiency reasons, I've already heard that in my previous thread (the link to which I forgot to copy) and I still seem to be learning from attempting to implement it).

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, the order of things you want is just not possible. Write the smart pointer now.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    24

    Thanks

    I think I've got it working, though I haven't found as many bugs as I expected. Still, that's my problem for now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a troublesome C++ program
    By Kristina in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2002, 01:28 PM