Avatar billede krismort Nybegynder
15. juni 2004 - 22:18 Der er 5 kommentarer

nøddeknækker

Jeg har 2 klasser C1 & C2, jeg vil gerne kalde en funktion i klasse C2 fra klasse C2 ved brug af en "omvej" gennem C1
jeg ville gøre det ved at bruge en template funktion men jeg kan ikke lige hitte ud af om det er umuligt at definerer en funktor gennem en instans man for fra en template til en funktion. Er det umuligt ? NB. Jeg er ikke interesseret i at lave C1 til en template class!

class C1 {
public:
 
  template<
    class T
  >
  void callFunction(std::string name, T *Instance, T::*pFunction) {
     
  }
}g_c1;

class C2 {
  C2() {
    g_c1.callFunction("fb1", this, foobar1);
    g_c1.callFunction("fb2", this, foobar2);
  }

  void foobar() {
    cout << "foobar" << endl;
  }

  void foobar2() {
    cout << "foobar2" << endl;
  }
};



nogen bud ? :)
Avatar billede bertelbrander Novice
15. juni 2004 - 22:43 #1
En ikke ret køn løsning:

#include <iostream>
#include <string>

class C1
{
public:
  void callFunction(std::string name, void *p, void (* PFunc)(void *))
  {
    std::cout << name << std::endl;
    PFunc(p);
  }
}g_c1;

class C2
{
public:
  C2()
  {
    g_c1.callFunction("fb1", this, foo1);
    g_c1.callFunction("fb2", this, foo2);
  }
  static void foo1(void *p)
  {
    ((C2 *)p)->foobar1();
  }

  static void foo2(void *p)
  {
    ((C2 *)p)->foobar2();
  }
  void foobar1()
  {
    std::cout << "foobar" << std::endl;
  }

  void foobar2()
  {
    std::cout << "foobar2" << std::endl;
  }
};

int main()
{
  C2 c2;
}
Avatar billede bertelbrander Novice
15. juni 2004 - 23:15 #2
Lidt kønnere løsning:
#include <iostream>
#include <string>

class C2Base
{
public:
  virtual void foobar1() = 0;
  virtual void foobar2() = 0;
};

class C1
{
public:
  void callFunction(std::string name, C2Base *Obj, void (C2Base::*PFunc)())
  {
    std::cout << name << std::endl;
    (Obj->*PFunc)();
  }
}g_c1;

class C2 : public C2Base
{
public:
  C2()
  {
    g_c1.callFunction("fb1", this, &C2Base::foobar1);
    g_c1.callFunction("fb2", this, &C2Base::foobar2);
  }
  void foobar1()
  {
    std::cout << "foobar" << std::endl;
  }

  void foobar2()
  {
    std::cout << "foobar2" << std::endl;
  }
};

int main()
{
  C2 c2;
}
Avatar billede bertelbrander Novice
15. juni 2004 - 23:23 #3
Og endelig en med template funktion:

#include <iostream>
#include <string>

class C2Base
{
public:
  virtual void foobar1() = 0;
  virtual void foobar2() = 0;
};

class C3Base
{
public:
  virtual void whatever() = 0;
  virtual void something() = 0;
};

class C1
{
public:
  template <class T>
  void callFunction(std::string name, T *Obj, void (T::*PFunc)())
  {
    std::cout << name << std::endl;
    (Obj->*PFunc)();
  }
}g_c1;

class C2 : public C2Base
{
public:
  C2()
  {
    g_c1.callFunction<C2Base>(std::string("fb1"), this, &C2Base::foobar1);
    g_c1.callFunction<C2Base>(std::string("fb2"), this, &C2Base::foobar2);
  }
  void foobar1()
  {
    std::cout << "foobar" << std::endl;
  }

  void foobar2()
  {
    std::cout << "foobar2" << std::endl;
  }
};

class C3 : public C3Base
{
public:
  C3()
  {
    g_c1.callFunction<C3Base>(std::string("What"), this, &C3Base::whatever);
    g_c1.callFunction<C3Base>(std::string("Some"), this, &C3Base::something);
  }
  void whatever()
  {
    std::cout << "Whatever" << std::endl;
  }

  void something()
  {
    std::cout << "Something" << std::endl;
  }
};

int main()
{
  C2 c2;
  C3 c3;
}
Avatar billede krismort Nybegynder
16. juni 2004 - 14:04 #4
tak skal du have det var lige hvad jeg havde brug for
Avatar billede bertelbrander Novice
16. juni 2004 - 23:06 #5
Ved nærmere eftertanke; man behøver ikke der virtuelle base class'er hvis man bruger template funktionen:

#include <iostream>
#include <string>

class C1
{
public:
  template <class T>
  void callFunction(std::string name, T *Obj, void (T::*PFunc)())
  {
    std::cout << name << std::endl;
    (Obj->*PFunc)();
  }
}g_c1;

class C2
{
public:
  C2()
  {
    g_c1.callFunction<C2>(std::string("fb1"), this, &C2::foobar1);
    g_c1.callFunction<C2>(std::string("fb2"), this, &C2::foobar2);
  }
  void foobar1()
  {
    std::cout << "foobar" << std::endl;
  }

  void foobar2()
  {
    std::cout << "foobar2" << std::endl;
  }
};

class C3
{
public:
  C3()
  {
    g_c1.callFunction<C3>(std::string("What"), this, &C3::whatever);
    g_c1.callFunction<C3>(std::string("Some"), this, &C3::something);
  }
  void whatever()
  {
    std::cout << "Whatever" << std::endl;
  }

  void something()
  {
    std::cout << "Something" << std::endl;
  }
};

int main()
{
  C2 c2;
  C3 c3;
}
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester