see these class:

class test
{
  public:
      void virtual Created(){}
      void test()
      {
         Created();
      }

};


class test1 : public test
{

  void Created();
  void test1(): test
  {
    Created();
  }

} test1;


void test1::Created()
{
   cout << "created test1";
}

(these code wasn't tested, but you get the point)
can i transform the test1 in a template?(continue using the object name like class name)
why transform that in template?
for be more quickly and then i just need created the Created() function outside the class\functions.

Recommended Answers

All 8 Replies

for the moment i have these code:

class test
{
public:
    virtual void Created(){}
    test()
    {
       Created();
    }
};

template <typename T>
class events : public T
{
public:
    events(){}
    ~events(){}

};

int main()
{
    events<test> a;
    return 0;
}

error messages:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|43|error: 'template<class T> class events' used without template parameters|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp||In function 'void events()':|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|43|error: 'void events()' redeclared as different kind of symbol|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|35|error: previous declaration of 'template<class T> class events'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|45|error: 'template<class T> class events' used without template parameters|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|48|error: 'template<class T> class events' used without template parameters|
||=== Build finished: 5 errors, 0 warnings (0 minutes, 2 seconds) ===|"

You need T used inside the class, like this:

template <class T>
class events : public T
{
public:
    events(){}
    ~events(){}
    bool operator==( const T& aTee ) const     // Explicit cast
    { return (aTee == *(static_cast<T*>(this)); }
    const T* getTpart() const { return this; } // Implicit cast
};

The comparison operator is a case where you would preferably use static_cast<T*> instead of a dynamic_cast<T*>.

i get these errors:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp||In member function 'bool events<T>::operator==(const T&) const':|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|25|error: expected ')' before ';' token|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 2 seconds) ===|"

i found the error, you forget ')'. heres the code corrected:

template <class T>
class events : public T
{
public:
    events(){}
    ~events(){}
    bool operator==( const T& aTee ) const     // Explicit cast
    { return (aTee == *(static_cast<T*>(this))); }
    const T* getTpart() const { return this; } // Implicit cast
};

now i need add another function for change the function and another one for call it.
imagine that i create the event in base class, the new class can overrride the the event function?

template <class T, class e>
class e : public T
{
public:
    e(){}
    ~e(){}
    bool operator==( const T& aTee ) const     // Explicit cast
    { return (aTee == *(static_cast<T*>(this))); }
    const T* getTpart() const { return this; } // Implicit cast
}e;

can i do these?

ok... imagine these code works and we create the event:

class test
{
    event a(int a);

    test()
    {
        raiseevent a(20);
    }   
};

how can i build the a() function ouside of class?
how can i have sure these event can works diferently for other objects?

why i can't do these?

template <class T, class A>
class events : public T
{
public:
    events(){}
    ~events(){}
    bool operator==( const T& aTee ) const     // Explicit cast
    { return (aTee == *(static_cast<T*>(this))); }
    const T* getTpart() const { return this; } // Implicit cast
}A;

error messages:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|15|error: declaration of 'events<T, A> A'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|6|error: shadows template parm 'class A'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|15|error: template declaration of 'events<T, A> A'|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 2 seconds) ===|"

in these i muse use the 'A' in that place. but why i can't and in normal class i can?

these class was more complicated.. so i did the other.
these topic i will put 'resolved', but i accept sugestions for these class;)
thanks to all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.