| View previous topic :: View next topic |
| Author |
Message |
Dark Byte Site Admin
Reputation: 472
Joined: 09 May 2003 Posts: 25870 Location: The netherlands
|
Posted: Fri Feb 08, 2008 5:59 pm Post subject: Delphi->c++ question (Object Oriented) |
|
|
Ok, In delphi when I have want a pointer to be used for dynamic routine picking inside a class I can use
| Code: |
type TMousePosRoutine=procedure(x,y: integer) of object;
...
class = TMyclass
private
public
MyRoutine: TMousePosRoutine;
Procedure xxx;
end;
...
procedure TMyClass.xxx;
begin
showmessage('Going to call a foreign class routine');
MyRoutine(12,13);
end;
|
and in the another class that uses a TMousePosRoutine I can set MyRoutine to a function in the class
| Code: |
class TMyOtherClass
private
theclass: TMyclass;
a,b: integer;
procedure something1(x,y: integer);
procedure something2(x,y: integer);
public
procedure dosomething;
procedure wtf;
end;
...
the constructor creates theclass
...
procedure TMyOtherClass.wtf;
begin
if random(0)=0 then
theclass.MyRoutine:=something1
else
theclass.MyRoutine:=something2;
end;
procedure TMyOtherClass.dosomething;
begin
a:=12;
b:=13;
wtf;
theclass.xxx;
end;
procedure TMyOtherClass.something1(x,y: integer);
begin
showmessage(inttostr(a+x+y));
end;
procedure TMyOtherClass.somethingb(x,y: integer);
begin
showmessage(inttostr(b+x+y));
end;
|
Now I'd like to know how something like this is possible on c++
I have a class and want to call a routine of another non-related class (only conforming is that the parameters are the same) and be able to access the variables of the class object the object belongs to
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 472
Joined: 09 May 2003 Posts: 25870 Location: The netherlands
|
Posted: Fri Feb 08, 2008 6:27 pm Post subject: |
|
|
half.
I need to register a callback in a Cxxx class that calls the callback routine defined in the Czomg class which has no inheritance or any links, besides having a pointer to the Cxxx class
e.g a non object oriented method would be:
| Code: |
typedef int (*MYCALLBACKTYPE) (int x, int y);
int myroutine(int x, int y)
{
return x*y+12;
}
...
MYCALLBACKTYPE mycallback;
mycallback=myroutine;
mycallback(12,13);
|
But I need it inside a class since the routine needs some variables from the class object, and i'm not going to use global variables
but I guess i'll just make the class build a bit more complex and add some templates for this kind of stuff
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping
Last edited by Dark Byte on Fri Feb 08, 2008 6:45 pm; edited 1 time in total |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Feb 08, 2008 6:42 pm Post subject: |
|
|
I am not quite sure what you are talking about, do you mean something like this?
| Code: | typedef int(*ADDFUNC)(int a, int b);
int imthefunction(int a, int b)
{
return a+b;
}
int main()
{
ADDFUNC add;
add = imthefunction;
add(2, 3);
} |
Edit: Err, didn't see your post, the forum was lagging bad when I was posting this.
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 472
Joined: 09 May 2003 Posts: 25870 Location: The netherlands
|
Posted: Fri Feb 08, 2008 6:46 pm Post subject: |
|
|
something like that yes, but then for c++
but I can't find much about it besides some extensive use of templates, so by default no possible
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Feb 08, 2008 6:52 pm Post subject: |
|
|
| Dark Byte wrote: | something like that yes, but then for c++
but I can't find much about it besides some extensive use of templates, so by default no possible |
Well, I think I found what you are looking for, but it does indeed use a template: http://www.partow.net/programming/templatecallback/index.html
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 472
Joined: 09 May 2003 Posts: 25870 Location: The netherlands
|
Posted: Fri Feb 08, 2008 6:57 pm Post subject: |
|
|
i've solved it in a disgusting way, but at least 'semi' OO
I gave the class the 'this' pointer and give the address of a callback stub.
:
| Code: |
inside Czomg:
int Czomg_routinex(Czomg *self, int x, int y)
{
return self->routinex(x,y);
}
Cxxx->callbackroutine=Czomg_routinex;
Cxxx->callbackclass=this;
...
inside Cxxx:
callbackroutine(callbackclass,x,y);
|
I know I could have given Cxxx knowlege about Czomg and just do callbackclass->routinex(x,y), but it can't know everything.
e.g Cxxx can be used by CBrick , CSpeed and CPosition, which none have anything in common, besides registering a specific callback event
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Sat Feb 09, 2008 7:54 am Post subject: |
|
|
I take it what i did in this example wont work?
| Code: |
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
class Math
{
public:
Math() { cout << endl << "Math constructer is being called" << endl; }
~Math() { cout << endl << "Math Destructer is being called" << endl; }
int Add(int one, int two){ one = one+two; return one; }
int Subtract(int one, int two) { subAnswer = one-two; return subAnswer; }
private:
int addAnswer, subAnswer;
};
class SomeClass
{
public:
SomeClass();
~SomeClass();
void Output(int one, int two);
private:
Math * newMath;
};
//SumClass Constructer / Destructer
SomeClass::SomeClass()
{
cout << endl << "Making new Math object" << endl;
newMath = new Math;
}
SomeClass::~SomeClass()
{
cout << endl << "Destroying Math object" << endl;
delete newMath;
newMath = 0;
}
// Output
void SomeClass::Output(int one, int two)
{
cout << "Adding" << one << " + " << two << " and it equals: " << newMath->Add(one, two) << endl;
cout << "Subtracting " << one << " - " << two << " and it equals: " << newMath->Subtract(one,two) << endl;
cout << "Adding " << two << " + (" << one << " - " << two << ") which equals: " << newMath->Add(two,newMath->Subtract(one,two)) << endl;
}
int main()
{
bool loop = 1;
cout << "Welcome to my test." << endl;
while(loop == 1)
{
int loopcontrol;
cout << "Enter 1 to continues, and 0 to exit: ";
cin >> loopcontrol;
switch(loopcontrol)
{
case 0:
cout << "Good Bye";
loop = 0;
break;
case 1:
int one = 0, two = 0;
SomeClass * test = new SomeClass;
cout << "First Number: ";
cin >> one;
cout << "Second Number: ";
cin >> two;
test->Output(one,two);
delete test;
test = 0;
break;
}
}
return EXIT_SUCCESS;
}
|
It is still just as bad as inheritance. Between vc++ and dev c++ there is over a 300 K diffrence in ram usage
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
|