www.Hacking-Romania.com
Hacking, gaby hacker team, programe hack, radmin hack, hi5 hack, hack the west, hacking romania, hacking stuff, hacking tools, 1 hack, 1st hacks, 2 hack, 2 hacks, 3 hack, 3 hacks, 3000 hack, 3004 hack, 4 hack, 4 hacks, 55 hack, 6 hack, 6 hacks, 7 hack, 7 hacks, 9 hack, 9dragons hack, a hack, adventure quest hack, aim hack, alz hack, and hacks, best hack, blue hack, bots hack, bots hacks, buy hack, cabal online hack, chaos hacks, cheat engine hack, cheat hack, cheats and hacks, cheats hacks, city hack, club hack, combo hack, conquer hacks, conquer online hack, conquer online hacks, conquer speed hack, conquiztador hack, counter strike 1.6 hack, damage hack, de hack, download hack, download hack for, dragonfable hack, dragonfable hacks, drakkarious hack, exp hack, flyff hack, free hack, free hacks, game hack, game hacks, garena exp hack, gladiatus hack, gm hack, gold hack, gunz hack, hack, hack 5, hack a pc, hack a site, hack a website, hack blog, hack conquer, hack counter strike, hack crack, hack cs, hack cs 1.6, hack dvd, hack email, hack forum, hack hunter, hack id, hack info, hack it, hack mess, hack muonline, hack net, hack password, hack passwords, hack pc, hack pdf, hack programs, hack site, hack sites, hack soft, hack software, hack team, hack the game, hack this, hack website, hack windows xp, hack world, hack xp, hacked, hacking, hacking game, hacking programs, hacking software, hacking tutorials, hacks, how hack, how to hack, icon hack, last chaos hack, last chaos hacks, life hack, lineage 2 hack, lineage 2 hacks, linux hack, lvl hack, maplestory hacks, mobile hack, multi hack 3.0, mybrute hack, naruto arena hack, naruto arena hacks, one hit kill hack, online hacks, perfect world hacks, pool hack, programe hack, resolution hack, resource hack, roll hack, royal hack, silkroad hack, source hack, speed hack, speed hacks, super hack, the west hack, warrock hack, warrock hacks, web hack, xpango hack, lockerz forum
Lista Forumurilor Pe Tematici
www.Hacking-Romania.com | Reguli | Inregistrare | Login

POZE WWW.HACKING-ROMANIA.COM

Nu sunteti logat.
Nou pe simpatie:
papiota55 din Bucuresti
Femeie
21 ani
Bucuresti
cauta Barbat
24 - 54 ani
www.Hacking-Romania.com / Programare / Basic C++ programming techniques  
Autor
Mesaj Pagini: 1
jocker
HACKING~TEAM

Inregistrat: acum 16 ani
Postari: 910
Incepem o serie in care vom discuta despre diverse tehnici de baza privind programarea in limbajul C++.


In acest snippet vom vedea cum putem simula mecanismul de polimorfism in momentul compilarii (compile time), folosind sabloane (templates) si o tehnica numita CRT (curiously recurrent template).

In mod normal, polimorfism-ul apare la runtime si se bazeaza pe mecanismul functiilor virtuale.

Sa vedem un examplu trivial:

Code:
// virtual .cpp
#include <iostream>

class Base
{
   public:
   
   virtual ~Base() {};
   
   virtual void func() = 0;
};

class Derived1 : public Base
{
   public:
   
   void func()
   {
      std::cout << "Hello from Derived1" << std::endl;
   }   
};

class Derived2 : public Base
{
   public:
   
   void func()
   {
      std::cout << "Hello from Derived2" << std::endl;
   }   
};


class Proxy
{
   public:
   Proxy(Base* param) : m_param(param) {}
   void SetParam(Base* param) { m_param = param; }
   void func() { if (m_param) m_param->func(); }
   
   private:
   Base* m_param;
};
   
int main()
{
   Derived1 myDerived1;
   Derived2 myDerived2;
   Proxy myProxy1(&myDerived1);
   myProxy1.func();
   Proxy myProxy2(&myDerived2);
   myProxy2.func();
   
   return 0;
}


Avem o clasa de baza denumita Base, din care deriveaza 2 clase Derived1 si Derived2.
In clasa de baza Base, avem declarata o functie membru virtual pura, numita func
Datorita faptului ca func este virtual pura, clasele derivate din Base vor trebui sa redefineasca functia func, altfel se va obtine o eroare la compilare.

Clasa Proxy are ca membru un pointer catre un obiect de tip Base (sau, evident, derivat din Base).

Daca compilam acest cod:

Code:
g++ virtual.cpp -o virtual


obtinem urmatorul output:

Code:
./virtual
Hello from Derived1
Hello from Derived2


Utilizarea functiilor virtuale aduce un grad mare de flexibilitate in design-ul programului, dar, aduce si un overhead: o indirectare in plus a apelului functiei virtuale prin tabelul de functii virtuale (vtable).
In marea majoritate a cazurilor, acest overhead este minor si poate fi ingnorat, dar, sunt aplicatii in care dorim performanta maxima si vrem sa eliminam acest overhead.

O solutie este utilizarea sabloanelor (templates) pentru a obtine un efect similar la compile time.
Sa vedem cum modificam exemplul precedent pentru a beneficia de aceasta tehnica.

Code:
// crtp.cpp
#include <iostream>

template <typename T>
class Base
{
   public:
   
   virtual ~Base() {};
   
   void func()
   {
      (static_cast<T*>(this))->func();
   }
};

class Derived1 : public Base<Derived1>
{
   public:
   
   void func()
   {
      std::cout << "Hello from Derived1" << std::endl;
   }   
};

class Derived2 : public Base<Derived2>
{
   public:
   
   void func()
   {
      std::cout << "Hello from Derived2" << std::endl;
   }   
};

template <typename T>
class Proxy
{
   public:
   Proxy(Base<T>* param) : m_param(param) {}
   void SetParam(Base<T>* param) { m_param = param; }
   void func() { if (m_param) m_param->func(); }
   
   private:
   Base<T>* m_param;
};
   
int main()
{
   Derived1 myDerived1;
   Derived2 myDerived2;
   Proxy<Derived1> myProxy1(&myDerived1);
   myProxy1.func();
   Proxy<Derived2> myProxy2(&myDerived2);
   myProxy2.func();
   
   return 0;
}


Clasa Base este acum o clasa sablon dupa parametrul T
Sa analizam implementare functiei membru func in clasa Base


Code:
void func()
{
   (static_cast<T*>(this))->func();
}


Aici, convertim pointerul this catre un pointer la tipul T (parametrul sablonului) si apelam functia func membra a tipului T. Ideea este faptul ca parametrul T este o clasa care are definita o functie membru numita func.
Bineinteles, vom avea grija sa instantiem corect sablonul Base

Sa analizam acum felul in care sunt declarate clasele derivate Derived1 si Derived2


Code:
class Derived1 : public Base<Derived1>
{
   public:
   
   void func()
   {
      std::cout << "Hello from Derived1" << std::endl;
   }   
};


Clasa Derived1 nu e declarata ca fiind o clasa sablon, dar deriveaza din clasa sablon Base instantiata chiar dupa tipul Derived .
Aceasta constructie poate parea complet neintuitiva unui programator novice, de aceea constructia este numita:
curiously recurrent template

Observam acum ca functia membra func nu este declarata ca fiind virtuala in clasele derivate Derived1 si Derived2

Daca apelam functia func() pe un obiect de tipul Derived1 (sau Derived2) prin intermediul unui pointer sau referinte la o instanta a clasei de baza (Base<Derived1> sau Base<Derived2>, se va apela implementarea lui func() din clasa de baza:


Code:
(static_cast<T*>(this))->func();


Dar, precum am vazut, in aceasta implementare noi convertim de fapt pointerul la instanta clasei de baza ((Base<Derived1> sau Base<Derived2> catre un pointer de tipul paramtrului sablonului dupa care s-a facut instantierea (Derived1 sau Derived2)
Deci, ca efect, ajungem sa apelam implementarea lui func() din clasa derivata (Derived1 sau Derived2)

In acest caz, clasa Proxy este tot o clasa sablon care mentine ca membru un pointer catre Base<T>.
Clasa Proxy este instantiata in functia main dupa Derived1 si Derived2

Compilare:

Code:
g++ crtp.cpp -o crtp

Executie:

Code:
./crtp
Hello from Derived1
Hello from Derived2


Aceasta tehnica este folosita de catre multe framework-uri moderne scrise folosind C++, precum WTL, SmartWin, Loki ...
Este o tehnica de baza in programarea moderna in limbajul C++.


_______________________________________
[img]http://i320.photobucket.com/albums/nn324/pad_vlad/moderator-3.gif[/img]

pus acum 16 ani
   
matzu
MEMBRU ACTIV

Din: Cluj
Inregistrat: acum 16 ani
Postari: 216
interesant 

pus acum 16 ani
   
stefanmizil
Pe lista neagra

Inregistrat: acum 16 ani
Postari: 192
Tare

pus acum 15 ani
   
andrew11287
SPAMMER

Inregistrat: acum 14 ani
Postari: 12
greeeu .... 

_______________________________________
[url]urlîhttp://www.google.ro/images?q=tbn:nMv0LQnvpbHFuM::hackstory.net/skins/common/images/banner-hackstory.png&h=26&w=143&usg=__6F0aMV69uLW3P8IRN7Jhhzssxzc=[/url]

pus acum 14 ani
   
danuhack
MEMBRU SPECIAL

Din: '"test
Inregistrat: acum 16 ani
Postari: 447
Bat la pariu ca nici unul din care au raspuns nu stiu ce eala c++

_______________________________________
[img]http://i220.photobucket.com/albums/dd214/CatalynCN/bnr_bancnota_zero_0_lei_ron_1266581.jpg[/img]

pus acum 14 ani
   
Pagini: 1  

Mergi la