×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Overloading cast operator with pointers

Overloading cast operator with pointers

Overloading cast operator with pointers

(OP)
Hi everyone - I am trying to overload the cast operator on a class.  However, I would like to do it with pointers.  For instance, I want:

     Class1* var1;
     Class2* var2;

     var2 = (Class2*)var1;

When the cast happens, I want to execute a chunk of code which will assert that Class1 is actually an instance of Class2.  I have seen lots of examples where the actual object cast can be overloaded, but not a pointer to the object.

Any thoughts?  Your help is always apprecitated!
-Tim

RE: Overloading cast operator with pointers

It is not often that one would need to do this, so use it with caution.  But the following should work.  Instead of
  var2 = (Class2*)var1;
just insert an additional cast to pointer to void
  var2 = (Class2*)((void*)var1);

Most compilers accept this.

Mark

RE: Overloading cast operator with pointers

(OP)
The var2 = (Class2*)((void*)var1) will work, however that assumes that I already know that var1 is an instance of Class2.  Obviously, if it's not, the first time I go to use var2 my program will crash.

When you overload the output operator "<<" on a class, for instance, you can tell it to do something different.  I want the "cast" operator to do something different in this case.  I could easily check in advance to see if var1 was an instance of Class2 and then do the cast as it is noted above, but after doing this 10 or so times, the code gets fairly large and a shorter way of converting would be useful.  

If only the compiler supported <dynamic_cast>... Ah, at times Java is the answer...
-Tim

RE: Overloading cast operator with pointers

Hi!
Here is a simple solution for your problem. This will generate a compilation error if you wan to cast to a wrong type.
For run-time checking use dynamic_cast. (enable RTTI in your compiler in this case!).

Class1
{
};

Class2 : public Class1
{
public:
 template<class T> operator T*()
 {
  return static_cast<T*>(this);
 }
};

Class3
{
};

void foo()
{
 Class1 *c1 = new Class1();
 Class2 *c2 = (Class1*)c1; //OK
 Class3 *c3 = (Class3*)c1; //generates a compilation error
}

RE: Overloading cast operator with pointers

You can write iterators for each of your classes. Iterators are more typesafe than pointers, and casting can be overloaded easily.

RE: Overloading cast operator with pointers

I am researching the same problem.

I would like to be able to say:

    class2 *ptr2 = (class2*)ptr1;

rather than:

    class2 *ptr2 = dynamic_cast<class2*>(ptr1);

and override it with both the dynamic cast and the assert.

However, I can not find a way to override an explicint (or implicit) cast.


zolle69's response would have been just perfect.

(I had to correct one line:

     Class2 *c2 = (Class1*)c1; //OK

to

    Class2 *c2 = (Class2*)c1; //OK


to get it to compile.)

However:

- it compiles under microsoft,visual c++ 7.0 , with no complaint,

- it runs with no complaint, (It should complain about the cast fo Class3),

- however it never executes the code in the template cast.

---

Has anyone found a way to overide a cast, so I can do checking

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources