SlideShare a Scribd company logo
1 of 10
Download to read offline
Virtual Functions


   By Roman Okolovich
ISO/IEC 14882:1998(E). International Standard.
First Edition 1998-09-01
   10.3 Virtual Functions
       10.3.1 Virtual Functions support dynamic binding and object-
        oriented programming. A class that declares or inherits a virtual
        function is called a polymorphic class.
       If a virtual member function vf is declared in a class Base and in a
        class Derived, derived directly or indirectly from Base, a member
        function vf with the same name and same parameter list as Base::vf
        is declared, then Derived::vf is also virtual (whether or not it is so
        declared) and it overrides Base::vf. For convenience we say that
        any virtual function overrides itself.

   C++ standard does not say anything about implementation of
    virtual functions. The compilers are free to implement it by
    any (correct) mechanism. However, practically majority of the
    compilers that exist today implement virtual functions using
    vtable mechanism.
Example
class Transaction                              // base class for all transactions
{
public:
    Transaction();
    virtual void logTransaction() const = 0;   // make type-dependent log entry
    //...
};
Transaction::Transaction()                     // implementation of base class constructor
{
    //...
    logTransaction();                          // as final action, log this transaction
}
class BuyTransaction : public Transaction      // derived class
{
public:
    virtual void logTransaction() const;       // how to log transactions of this type
    //...
};
class SellTransaction : public Transaction     // derived class
{
public:
    virtual void logTransaction() const;       // how to log transactions of this type
    //...
};
BuyTransaction b;
General classes
class A                        at the code generation
{                                level:
   int foo();                  struct D
                               {
  int a1;                        int a1;
  int a2;                        int a2;
  int a3;
                                 int a3;
};                               int b1;
class B                          int b2;
{                                int b3;
   int bar();                    int d1;
                                 int d2;
  int b1;                        int d3;
  int b2;                      };
  int b3;                      ?foo@A@@ModuleA@(A* pThis);
};                             ?bar@B@@ModuleB@(B* pThis);
class D : public A, public B   ?foo@D@@ModuleD@(D* pThis);
{
   int foo();                  D::D() {}
  int d1;
  int d2;                      A::foo( (A*)&D );
  int d3;
};
A* pA = new D;
pA->foo();
Virtual Functions
class Vbase                  at the code generation level:
                             struct Vbase
{                            {                                      static struct VbTable
     virtual void foo();        void* m_pVptr; //                  {
     virtual void bar();        int a1;                               void (*foo)();
                             };
     int a1;                 ?foo@Vbase@@ModuleVb@(Vbase* pThis);     void (*bar)();
                             ?bar@Vbase@@ModuleVb@(Vbase* pThis);   };
};
                             cBase.m_pVptr[foo_idx](&cBase);

Vbase cBase;                 struct V
                             {
cBase.foo();                    void* m_pVptr; //                  static   struct VTable
                                int a1;                             {
                                int b1;                               void   (*foo)();
                             };                                       void   (*bar)();
class V : public Vbase                                                void   (*dir)();
                             ?foo@V@@ModuleV@(V* pThis);              void   (*alpha)();
{                            ?dir@V@@ModuleV@(V* pThis);
                             ?alpha@V@@ModuleV@(V* pThis);          };
     void foo();
     virtual void dir();
     virtual void alpha();
     int b1;
};
Class Constructors
class Vbase                 at the code generation level:
{                           Vbase::Vbase
  virtual void foo();       {
  virtual void bar();         m_pVptr->[0] = void(*foo)();
  virtual void dir() = 0;     m_pVptr->[1] = void(*bar)();
     void do()                m_pVptr->[2] = 0;
     {                      };
       foo();
     }
     int a1;                V::V : Vbase()
                            {
};                            m_pVptr->[0]   =   void(*foo)();
                              m_pVptr->[1]   =   void(*Vbase::bar)();
                              m_pVptr->[2]   =   void(*dir)();
class V : public Vbase        m_pVptr->[3]   =   void(*alpha)();
{
                                Vbase::m_pVptr = m_pVptr;
   void foo();              }
   virtual void dir();
   virtual void alpha();    V objV;
   int b1;                  objV.do(Vbase* this)
};                          {
                              this->foo(); // this  Vbase
                              // foo(this);
                            }
__declspec(novtable)
class __declspec(novtable) IBase               at the code generation level:
{
   virtual void foo() = 0;                     IBase::IBase
   virtual void bar() = 0;                     {
   virtual void dir() = 0;
                                                 //m_pVptr
};                                               // will not be initialized at all
class V : public IBase                         };
{
   void foo();
   virtual void bar();
   virtual void dir();
   int b1;
};

class __declspec(novtable) A
{
public:
     A() {};
     virtual void func1(){ std::cout << 1; }
     virtual void func2(){ std::cout << 2; }
     virtual void func3(){ std::cout << 3; }
  };
class A1 : public A
{
public:
     // no func1, func2, or func3
};
Default values
#include <iostream>                            at the code generation level:
using namespace std;
                                               struct VA
                                               {
class A {                                        void* m_pVptr;
                                               };
public:
    virtual void Foo(int n = 10) {             ?foo@A@@ModuleA@(VA* pThis, 10);
        cout << "A::Foo, n = " << n << endl;   struct VB
    }                                          {
                                                 void* m_pVptr;
};                                             };

class B : public A {                           ?foo@B@@ModuleB@(VB* pThis, 20);
public:
    virtual void Foo(int n = 20) {
        cout << "B::Foo, n = " << n << endl;   A * pa = new B();
    }                                          pa->m_pVptr[fooID]((VA*)&B, XXX);
};
                                               Answer: 10
int main() {
    A * pa = new B();
    pa->Foo();
    return 0;
}
Things to Remember
   Don't call virtual functions during construction or
    destruction, because such calls will never go to a more
    derived class than that of the currently executing
    constructor or destructor.
References
   PDF: Working draft, Standard for Programming
    Language C++

More Related Content

What's hot

pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cppgourav kottawar
 
virtual function
virtual functionvirtual function
virtual functionVENNILAV6
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)Kai-Feng Chou
 
OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)Kai-Feng Chou
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Liju Thomas
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Ismar Silveira
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrencyxu liwei
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 

What's hot (20)

pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
virtual function
virtual functionvirtual function
virtual function
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)
 
OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
C++ Training
C++ TrainingC++ Training
C++ Training
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
C++11
C++11C++11
C++11
 
C++11
C++11C++11
C++11
 
C++11
C++11C++11
C++11
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 

Viewers also liked

OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)Kai-Feng Chou
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism023henil
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsPraveen Penumathsa
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Sanjit Shaw
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...cprogrammings
 

Viewers also liked (20)

16 virtual function
16 virtual function16 virtual function
16 virtual function
 
OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented Programs
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
polymorphism
polymorphism polymorphism
polymorphism
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Stack
StackStack
Stack
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
 

Similar to Virtual Functions

Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphismmohamed sikander
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler supportSyed Zaid Irshad
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)Ananda Kumar HN
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)Olve Maudal
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13Chris Ohk
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11Uilian Ries
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfarjuncorner565
 
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data TypesEelco Visser
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1Little Tukta Lita
 

Similar to Virtual Functions (20)

Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler support
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Lab3
Lab3Lab3
Lab3
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
Codejunk Ignitesd
Codejunk IgnitesdCodejunk Ignitesd
Codejunk Ignitesd
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 
Composite Pattern
Composite PatternComposite Pattern
Composite Pattern
 
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data Types
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
 
Java byte code in practice
Java byte code in practiceJava byte code in practice
Java byte code in practice
 

More from Roman Okolovich

More from Roman Okolovich (11)

Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
C# XML documentation
C# XML documentationC# XML documentation
C# XML documentation
 
code analysis for c++
code analysis for c++code analysis for c++
code analysis for c++
 
Using QString effectively
Using QString effectivelyUsing QString effectively
Using QString effectively
 
Ram Disk
Ram DiskRam Disk
Ram Disk
 
64 bits for developers
64 bits for developers64 bits for developers
64 bits for developers
 
Visual Studio 2008 Overview
Visual Studio 2008 OverviewVisual Studio 2008 Overview
Visual Studio 2008 Overview
 
State Machine Framework
State Machine FrameworkState Machine Framework
State Machine Framework
 
The Big Three
The Big ThreeThe Big Three
The Big Three
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
Smart Pointers
Smart PointersSmart Pointers
Smart Pointers
 

Recently uploaded

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 

Recently uploaded (20)

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 

Virtual Functions

  • 1. Virtual Functions By Roman Okolovich
  • 2. ISO/IEC 14882:1998(E). International Standard. First Edition 1998-09-01  10.3 Virtual Functions  10.3.1 Virtual Functions support dynamic binding and object- oriented programming. A class that declares or inherits a virtual function is called a polymorphic class.  If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf. For convenience we say that any virtual function overrides itself.  C++ standard does not say anything about implementation of virtual functions. The compilers are free to implement it by any (correct) mechanism. However, practically majority of the compilers that exist today implement virtual functions using vtable mechanism.
  • 3. Example class Transaction // base class for all transactions { public: Transaction(); virtual void logTransaction() const = 0; // make type-dependent log entry //... }; Transaction::Transaction() // implementation of base class constructor { //... logTransaction(); // as final action, log this transaction } class BuyTransaction : public Transaction // derived class { public: virtual void logTransaction() const; // how to log transactions of this type //... }; class SellTransaction : public Transaction // derived class { public: virtual void logTransaction() const; // how to log transactions of this type //... }; BuyTransaction b;
  • 4. General classes class A at the code generation { level: int foo(); struct D { int a1; int a1; int a2; int a2; int a3; int a3; }; int b1; class B int b2; { int b3; int bar(); int d1; int d2; int b1; int d3; int b2; }; int b3; ?foo@A@@ModuleA@(A* pThis); }; ?bar@B@@ModuleB@(B* pThis); class D : public A, public B ?foo@D@@ModuleD@(D* pThis); { int foo(); D::D() {} int d1; int d2; A::foo( (A*)&D ); int d3; }; A* pA = new D; pA->foo();
  • 5. Virtual Functions class Vbase at the code generation level: struct Vbase { { static struct VbTable virtual void foo(); void* m_pVptr; //  { virtual void bar(); int a1; void (*foo)(); }; int a1; ?foo@Vbase@@ModuleVb@(Vbase* pThis); void (*bar)(); ?bar@Vbase@@ModuleVb@(Vbase* pThis); }; }; cBase.m_pVptr[foo_idx](&cBase); Vbase cBase; struct V { cBase.foo(); void* m_pVptr; //  static struct VTable int a1; { int b1; void (*foo)(); }; void (*bar)(); class V : public Vbase void (*dir)(); ?foo@V@@ModuleV@(V* pThis); void (*alpha)(); { ?dir@V@@ModuleV@(V* pThis); ?alpha@V@@ModuleV@(V* pThis); }; void foo(); virtual void dir(); virtual void alpha(); int b1; };
  • 6. Class Constructors class Vbase at the code generation level: { Vbase::Vbase virtual void foo(); { virtual void bar(); m_pVptr->[0] = void(*foo)(); virtual void dir() = 0; m_pVptr->[1] = void(*bar)(); void do() m_pVptr->[2] = 0; { }; foo(); } int a1; V::V : Vbase() { }; m_pVptr->[0] = void(*foo)(); m_pVptr->[1] = void(*Vbase::bar)(); m_pVptr->[2] = void(*dir)(); class V : public Vbase m_pVptr->[3] = void(*alpha)(); { Vbase::m_pVptr = m_pVptr; void foo(); } virtual void dir(); virtual void alpha(); V objV; int b1; objV.do(Vbase* this) }; { this->foo(); // this  Vbase // foo(this); }
  • 7. __declspec(novtable) class __declspec(novtable) IBase at the code generation level: { virtual void foo() = 0; IBase::IBase virtual void bar() = 0; { virtual void dir() = 0; //m_pVptr }; // will not be initialized at all class V : public IBase }; { void foo(); virtual void bar(); virtual void dir(); int b1; }; class __declspec(novtable) A { public: A() {}; virtual void func1(){ std::cout << 1; } virtual void func2(){ std::cout << 2; } virtual void func3(){ std::cout << 3; } }; class A1 : public A { public: // no func1, func2, or func3 };
  • 8. Default values #include <iostream> at the code generation level: using namespace std; struct VA { class A { void* m_pVptr; }; public: virtual void Foo(int n = 10) { ?foo@A@@ModuleA@(VA* pThis, 10); cout << "A::Foo, n = " << n << endl; struct VB } { void* m_pVptr; }; }; class B : public A { ?foo@B@@ModuleB@(VB* pThis, 20); public: virtual void Foo(int n = 20) { cout << "B::Foo, n = " << n << endl; A * pa = new B(); } pa->m_pVptr[fooID]((VA*)&B, XXX); }; Answer: 10 int main() { A * pa = new B(); pa->Foo(); return 0; }
  • 9. Things to Remember  Don't call virtual functions during construction or destruction, because such calls will never go to a more derived class than that of the currently executing constructor or destructor.
  • 10. References  PDF: Working draft, Standard for Programming Language C++