Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overloading cast operator with pointers

Status
Not open for further replies.

Guest
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
 
Replies continue below

Recommended for you

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
 
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 &quot;<<&quot; on a class, for instance, you can tell it to do something different. I want the &quot;cast&quot; 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
 
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
}
 
You can write iterators for each of your classes. Iterators are more typesafe than pointers, and casting can be overloaded easily.
 
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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor