Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hello,everyone.I saw a C++ interview questions,there are several issues do not understand, I hope someone can answer my questions,the following code is the code of interview questions,my question is as follows:

problem 1:A class object pA is assigned a NULL,as far as I know,when compile an class will have a member of the space,so that it can properly call a member function pointer,but now correctly call pA->Function procedure,why can be call correctly pA->Function,and the program did not crash。

problem 2:why do class B member function Function was declared as a virtual function,call in the same way。now the program crash。
C++
#include <cstdio>
class A
{
public:
    void Function()
    {
        printf("class A Function execute test!\n");
    }
};
class B
{
public:
    virtual void Function()
    {
        printf("class B Function execute test!\n");
    }
};
int main()
{
    A *pA=NULL;     
    pA->Function(); //why can correctly call
    B *pB=NULL;
    pB->Function(); //why crash here
    return 0;
}

Thank everyone for!
Posted
Updated 29-Mar-12 8:15am
v3

You need to instantiate (construct) an instance of the class using new A(); new B(); actually, as per your code, in both cases it won't work; is is not even related with OPP as you simply try to re-reference NULL.

[EDIT]

The code actually works as OP described with C++ of VS 2008. Please see the explanation below, in my comments.
Sorry for some confusion.

—SA
 
Share this answer
 
v3
Comments
C++Kernel 29-Mar-12 15:08pm    
I think you did not know what I mean,I want to know why the above code to work correctly,the program does not crash.
Sergey Alexandrovich Kryukov 29-Mar-12 15:59pm    
It does not really matter what do you mean; you code is pure gibberish. Also, you apparently misreport it: it crashed. Did you submit different code, not that you used in your testing.

Unfortunately, you don't understand some bare basics, in this case, the pointers, forget about OOP. Too bad, sorry for you.
--SA
C++Kernel 29-Mar-12 16:06pm    
I did not understand your work mean,I think you need to compile the above code,run about, you may understand my question,thank you for answer!
Sergey Alexandrovich Kryukov 29-Mar-12 16:08pm    
Sorry, pure gibberish does not need testing.
--SA
[no name] 29-Mar-12 17:36pm    
I agree with you. As soon as you read the code in main() who cares what it does. That would be my answer. This just confirms to me what a lot of bs some of these interviews are.
If you're asking this question, you're ovbiously new to C++.

Allow me to recommend a free book (in two volumes), that cover the basics and beyond, and explain the 'why' and 'how' of C++:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html[^]

It helped me a lot a few years ago, when I was moving from C to C++.

About your question: a non-virtual member function is just a C function that receives as its first parameter a pointer to 'this'.
A virtual call is a different kind of beast: at run time the VTABLE is searched for the correct function. From your question I assume you are not familiar with the concept of VTABLE; don't go to any job interviews until you are.

Welcome on board,

Pablo.
 
Share this answer
 
Comments
C++Kernel 29-Mar-12 15:15pm    
the above procedure should start form Class A crash,why did not crash, Class A object pA pointer to an emoty address,I think that is wrong,because it requires a space to hold the address of member,here is empty,why we can to run correctly.
Sergey Alexandrovich Kryukov 29-Mar-12 16:04pm    
I voted 5. This OP is extremely illiterate and aggressive (those qualities are often correlate), which might explain the down-voting of our answers.
Just one note: OP de-references NULL pointer, which will throw exception even for non-virtual method. OP's "did not crash" is pure lie (as if everything else would be not enough :-), but I suspect this lie is unintentional, due to mess-up in code and question text and general ignorance.

If our answers cannot help this time, I suggest black-list it to never help again; this could be just useless.
--SA
When calling a non-virtual function the compiler and linker know the address of the function already at compile / link time. Hence the function call pA->Function does work. However, you will not be able to use any member variables of A in that function, because the this-pointer is still pointing to an invalid address. The printf call in that function does not use the this-pointer, so that's ok. I am not sure, if that behavior is guaranteed by the C++ standard, but it works on many C++ implementations, within others Visual C++.

The second example pB->Function calls a virtual function. As already explained by Pablo in the previous entry, calling a virtual function makes use of the so-called vtable, a table with all the addresses of the virtual functions of a class. In most implementation the first few bytes of the class object are used as pointer to the vtable. And that is why this example must fail: We don't have a class object and so the access to the vtable fails.

I hope that made it clear why A works and B doesn't.
 
Share this answer
 
v2
Comments
C++Kernel 29-Mar-12 15:27pm    
thank you given answer!
Philippe Mori 29-Mar-12 18:15pm    
Although it appears to works, I think it is undefined behaviours according to the standard and for sure it is a bad coding practice to do it.
Sergey Alexandrovich Kryukov 29-Mar-12 18:55pm    
Absolutely right. Basically, this explanation is correct, but this is not something one can ever use.
--SA
Monjurul Habib 30-Mar-12 4:52am    
5!

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