Thread: question on syntax

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    question on syntax

    hi...i have questios on some syntax.

    look a snippet..

    Code:
    class page
    {....
     // blah
    
    
    
    virtual ~ page1();   // why virtual here ?  ------ // line1

    another code.....

    Code:
    class A
    {
    
     func1();
    
    
    
    }
    
    
    class B:public A
    {
    
    fun2();
    
    }
    
    
    B b;
    
    b.A :: func1() // is it legal ? what does it mean ?  //line  2

    my question
    ----------------

    Q1. from // line 1, i see that is a destructor. but why virtual ? virtual is usued in inheritance .why in a class defunition ? is it just a syntax ?

    Q2. look the // line 2 . i dont understand how this acessing is done. is that syntax ok ?

    i know only this rule to acess a memeber function.

    rule is objectname. functionname() .

    but above syntax is peuliar ? can anybody give some comment how that works (if it is legal at all ).

    the questions are based from the archives.
    blue_gene

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Q1. from // line 1, i see that is a destructor. but why virtual ? virtual is usued in inheritance .why in a class defunition ? is it just a syntax ?
    You really need virtual functions whenever the class will be used as base class(the rule is if the class has any other virtual function, add the virtual destructor) . Fortunatly, the compilers I've used are able to flag this as a warning.

    The reason why you need a virtual destructor is shown by this code
    Code:
    #include <iostream>
    using namespace std;
    
    class A {
    public:
          virtual void a() { cout << "A" << endl; }
          // no virtual destructor
    };
     
    class B {
          int* items;
    public:
          B() : items(new int[100]) { }
          ~B() { delete[] items; }
          virtual void a() { cout << "B" << endl; }
    };
    
    int main()
    {
         A* a = new B;
     
         delete a;           // since a's destructor is not virtual, A's destructor is called not B's
    
         return 0;
    }
    b.A :: func1() // is it legal ? what does it mean ? //line 2
    I don't think so.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I think what you mean by the code at the end is a call to the base class's function. You do that with something like this

    Code:
    class Sprite {
    public:
           virtual void draw() {/*...*/ }
    };
    
    class BdSprite : public Sprite {
    public:
           virtual void draw() { Sprite::draw(); }
    };
    This calls Sprite's draw, not BdSprite's, avoiding the infinite recursion that would otherwise occur.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax question: &(++x)
    By yoshiznit123 in forum C Programming
    Replies: 8
    Last Post: 06-02-2006, 10:40 PM
  2. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM