Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
May be the below questions are a bit naive, but they are confusing me as so I asked.

There 3 questions here below:
1) When does actually a virtual table gets created at compile time or runtime?
C++
class base
{
    int b;
public:
    base()
    {
        printf("Base Cons\n");
    }

    virtual void show(void)
    {
        printf("Base Show\n");
    }
};

class der: public base
{
    int d;
public:
    der()
    {
        printf("Der Cons\n");
    }

    void show(void)
    {
        printf("Der Show\n");
    }
};

void fn_base(void)
{
    base bobj;
    .
    .
    .
}

void fn_der(void)
{
    der dobj;
    .
    .
    .
}

int _tmain(int argc, _TCHAR* argv[])
{
    int option = 0;
    scanf("%d",&option);

    if(option == 1)
    {
        fn_base();
    }
    else
    {
        fn_der();
    }

    return 0;
}

In the above example the object will be created at runtime so will when does actually a virtual table gets created at compile time when I define the class or runtime when I create the objects?


2) We know sizeof is a compile time operator.
Consider below the same above example but modified a little:
C++
class base
{
    int b;
public:
    base()
    {
        printf("Base Cons\n");
    }

    virtual void show(void)
    {
        printf("Base Show\n");
    }
};

class der: public base
{
    int d;
public:
    der()
    {
        printf("Der Cons\n");
    }

    void show(void)
    {
        printf("Der Show\n");
    }
};

void fn_base(void)
{
    base bobj;
    printf("%d\n",sizeof(bobj));
}

void fn_der(void)
{
    der dobj;
    printf("%d\n",sizeof(dobj));
}

int _tmain(int argc, _TCHAR* argv[])
{
    int option = 0;
    scanf("%d",&option);

    if(option == 1)
    {
        fn_base();
    }
    else
    {
        fn_der();
    }

    return 0;
}


Above we are calling sizeof() in fn_base() and fn_der() at runtime.
But sizeof being a runtime operator how will it return the size when called from functions at runtime?


3) I have read that it gets created when we create an object of the class having virtual function. Will it get created even if I do not create any object of the class having virtual function?
Consider the following example:
C++
class base
{
    int b;
public:
    base()
    {
        printf("Base Cons\n");
    }

    virtual void show(void)
    {
        printf("Base Show\n");
    }
};

class der: public base
{
    int d;
public:
    der()
    {
        printf("Der Cons\n");
    }

    void show(void)
    {
        printf("Der Show\n");
    }

};

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}


Will the virtual table get created here?
Posted

1 solution

1. AFAIK the object files contain the layout of all classes, including their static members and vtables. At runtime, once the first instance of a specific class gets constructed, the vtable for that class will be constructed as well. Since the vtable is only needed once per class, it is not part of the class for purposes such as memory allocation (or sizeof()).

2. sizeof() gets evaluated at compile time, by the type if you pass a type, by the type of a variable if you pass one, or by the result type of an expression, if you pass that. There's no runtime evaluation. Note that even for a reference, the type of variable is important, not the type actally referenced at the time of function call.

3. I'm not sure but I suspect that a compiler must create object code for a class, even if it doesn't get used within a compilation unit. The reason is that in a different compilation unit, the declaration of that class could be copied, and then objects created and used, although the definition is contained elsewhere. (remember that header files do just that: they copy class declarations into cpp files!). The same is true for vtables: even though in the local compilation unit, a vtable (or rather the layout of one) may not be required, but it may very well be needed in another compilation unit.
 
Share this answer
 
Comments
rupeshkp728 21-Nov-11 9:30am    
Thanks Stefan for the reply.
But I am not able to understand clearly.
1) From your statement "At runtime, once the first instance of a specific class gets constructed, the vtable for that class will be constructed as well."
I understand the vtables will be created at runtime.

2) Based on understanding from reply to question 1, the sizeof() in fn_base() and fn_der() must be called at runtime for question two depending upon which function got called.
But I do not think it is functioning so.


I saw a good link which clears most of my doubt:
http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/

The link says "This table is simply a static array that the compiler sets up at compile time."
So even the sizeof() works at compile time and so it is able to get the proper size of class containing virtual functions.
Stefan_Lang 21-Nov-11 9:47am    
1) Depends what you mean by 'construction'. At compile time, the compiler of course needs to create a layout of a vtable and put it into the object code, so if the class ever gets instantiated, it can use that layout to then create it in memory. That's two steps of construction, once in the object code, and another time in memory. But the latter is not an automatic (see 3)

2. As I said, it's the types of the variables you pass as argument to sizeof that determines the result, not the object created at runtime!
rupeshkp728 21-Nov-11 10:20am    
Now the doubt for question clears for question 1.

I guess I am not able to properly express second question.
In second question I want to know if the sizeof(in functions fn_base()/fn_der()) will be called at compile time or run time.

I am asking so because the functions fn_base()/fn_der() as per the code flow will be called at runtime and then sizeof() is supposed to be called from
within them.
But sizeof() being a compile time operator will it be called from within fn_base()/fn_der() afterwards at runtime?
Philippe Mori 21-Nov-11 18:58pm    
sizeof is a compile time operator... so the compiler will evaluate the size and put it instead of the expression.

Assuming that the size of base is 8, then the printf in fn_base would be: printf("%d\n", 8);

There is no sizeof function executed at run time.
rupeshkp728 22-Nov-11 3:41am    
Thanks Phillipe for the reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900