分享

Autodesk 的 c 题(2007.05) - 面试总结 - 坚持到底

 ShaneWu 2009-12-08
Autodesk 的 c++题(2007.05)

1. What and why is explicit constructor and explicit copy constructor?

2. What the difference between Struct and Class?

3. What is vitrual function and why we need vtable?

1.

Explicit constructor and copy constructor is used to preventing implicit type cast.

An explicit constructor can not take part in implicit conversions. It can only be  used to explicit construct an object

Consider the Class below:

class A

{

public:

A();

A(int i);

A(A& a);

int i;

virtual ~A();

};

The following program will success to compile :

A a = 10;

or

A a;

a = 10;

because A has a constructor that accept a Int as a arguments

Those codes above are equal to

A tmp(10);    //constructor

a(tmp);      //copy constructor

or

A tmp(10) ;  //constructor

a = tmp;     // operator =

Note: explicit on a constructor with mutiple arguments has no effect,  unless all  but one of the arguments has a default value !

2: differences between struct and class?

3 difference between struct and class

1)members in struct are by default public while members in class are by default  private.

2)inhert  from  a struct is by default public , inhert from a class is by default private.

For example

struct A

{

};

struct B : (public)A

{

} ;

class A

{

};

class B: (private)A

{

};

3)class can be used as typename in template while struct can not

template(class T)      //right

template(struct T)     //wrong

3: What is virtual function and why we need vtable?

1) a virtual function is a function member of a class decleared with a virtual  keyword

2) it usually has a different functionality in the derived class

3) A function is resolved at run-time

The main difference between a non_virtual function and a virtual  function is, the  non_virtual function is resolved at compile time. This  mechanism is called  static_binding.  As the virtual function is  resolved at run time, This mechanism is  know as dynamic_binding

v_table is kind of lookup table.It contains pointers to the virtual  functions provided  by a class, and each object of the class contains a  pointer to its v_table(or  vtables, in some multiple-inheritance  situations)

A v_table is created at compile time,  so whether  a object is referenced by any  pointers or reference, when a virtual  fall happens, the v_table can help the object  find the exact function  address.

 原文地址 http://www./career/?p=17465

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多