Cast Question

Hi,

I have a problem with casting. Following Class:

Class One
{
public:
method1(){};

Two* ptr;
}

Class Two
{
public:
method2(){};
}

Now I construct an object of class One and an object of class Two

Two two;
One one;

Object one stores the pointer of obejct two. Is there a way to realize following behaviour:
one->method2;

so that the object one executes the method of object two?

I have tryed to add cast operator in the class One, but it does not work:
Class One
{
public
operator Two*() {return ptr};
method1(){};
Two* ptr;
}

Thank you!



You cannot magically cast one object into another1. Fortunately, there's no need whatsoever to do that. You can just dereference the pointer to get the object being pointed at.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

class ClassTwo
{
public:
  void method2(){std::cout << "I am a ClassTwo method!" << std::endl;};
};


class ClassOne
{
public:
  void method1(){};

ClassTwo* ptr;
};



int main()
{
  

// create object of type ClassOne
ClassOne someObject;

// create object of type ClassTwo
ClassTwo someOtherObject;

// make ClassOne's pointer point at the second object
someObject.ptr = &someOtherObject;

// Use the first object to call the method in the second object
someObject.ptr->method2();
// Alternative notation for the same thing
(*someObject.ptr).method2();

return 0;

}


1. Actually, you can, but it's telling the compiler to trust you and the compiler will happily let you go very badly wrong. It's dangerous and asking for trouble (and wouldn't work in this case either).
Last edited on
Thank you very much for the quick answer.

But I would like to achieve it without using the pointer in Class One.

Classoneobject->method1;
Classoneobject->method2;

What I would like to do is: the code automatically chooses the right method. If the object has the method, it runs it. If it doesn't find it, it casts itself to Classtwoobject and tries to find the method there.
As you have it implemented you can do this...

1
2
Classoneobject.method1();
Classoneobject.ptr->method2();


Edit* Moschops already gave the answer.

But I would like to achieve it without using the pointer in Class One.


Then you need a common method... doOperation() and a pointer to the base.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Operation
{
   public:
      virtual void doOperation()=0;
}
class ClassTwo : public Operation
{
public:
  void method2(){std::cout << "I am a ClassTwo method!" << std::endl;};
  void doOperation(){ method2(); }
};


class ClassOne : public Operation
{
public:
  void method1(){};
   void doOperation(){ method1(); }

ClassTwo* ptr;
};



You can also do this with static polymorphism, however, what you are asking doesn't make sense for ClassOne and ClassTwo they have no 'has a' or 'is a' relationship.
Last edited on
What I would like to do is: the code automatically chooses the right method. If the object has the method, it runs it. If it doesn't find it, it casts itself to Classtwoobject and tries to find the method there.


Are you asking the compiler to notice that you're calling a function that doesn't exist, and to then go searching through all the other objects it knows about, and find one that contains a function with the same name, and to call that function instead? That is insane.

Are you perhaps thinking of inheritance and virtual functions (as Clanmjc has written)?
Last edited on
Thank you very much for your help!

I will then try it with inheritance.
Topic archived. No new replies allowed.