人気のあるC ++インタビューの質問と回答

こんにちは

遅かれ早かれプログラミングに従事する人々は、潜在的な雇用者との技術面接を受ける必要に直面しています。

この投稿では、C ++プログラマーとのインタビューで求められること、およびこれらの質問への回答について説明します。

まえがき


質問と回答の形式の小さな導入部分で、いくつかのポイントを明確にし、この投稿で考えられる質問に答えようとします。

なんでこんなこと?
1つの投稿でそれらに対する実際の質問と回答を収集したいと思います。 なぜなら インターネットで見たものは好きではありません。時々ぼやけすぎたり(たとえば、ゲーム開発者の75ページ)、いくつかの例を検討したり、誰も修正できないエラーがあったり、すでに死んだトピックへのリンクがあったり、遠い...リストが続きます。

一般に、私はこのトピックに関する最も人気のある質問のリストを用意したいと思います。急にメモを更新したり、IDEやGoogleを実行したり、 1つの場所を開いてそこのすべてを読む必要がないように、知識を更新する必要がある場合

このトピックは、 C ++と呼ばれるこの無限の海で歩み始めたばかりの人にとって最も興味深いと思われるはずです。

質問はどこから来ますか?
私の小さな経験、私が知っている人々の経験から、そしてあなたの経験や友人の経験から多くの興味深いものを提供してくれることを願っています。

これらの問題の構造はどうなりますか?
行リスト。 質問が利用可能になると追加されます。このセクションを削除して更新しないようにします。 最後に、リストが最後に変更された日時が明確になるように、最後の更新の日付を設定します。

ここで、最初の20の質問を作成する予定です。このトピックに需要がある場合は、徐々に新しい質問を追加します。

コードエラーがありますが、どのように見落としていましたか?
人々は間違いを犯す傾向があり、私は自分自身を正確に書くことができる第一人者とは考えていません。 そしてこれはC ++であり、ここでは間違いの可能性がさらに大きくなります。

間違いを見つけたら、それについて書いてください。それを修正します。

質疑応答


1.なぜ仮想デストラクタが必要なのですか?

回答:オペレーションロジックにデストラクタへの呼び出しが含まれるオブジェクトのリソースまたは他の制御されない動作の漏れを回避するため。
例:
class Base
{
public:
    virtual ~Base()
    {
        std::cout << "Hello from ~Base()" << std::endl;
    }
};

class Derived : public Base
{
public:
    virtual ~Derived()
    {
        //      
        std::cout << "Hello from ~Derived()" << std::endl;
    }
};

Base *obj = new Derived();
delete obj;

Output:
Hello from ~Derived()
Hello from ~Base()


virtual Base . .. ~Base():

Output:
Hello from ~Base()

GooRoo . .

2. ?

: , c , , , , .
:
class Base
{
private: 
    HANDLE m_hFile;

public:
    Base()
    {
        std::cout << "Hello from Base()" << std::endl;
        m_hFile = ::CreateFileA(...);
        //  ,       
        SomeLib.SomeFunc(...);
    }

    virtual ~Base()
    {
        std::cout << "Hello from ~Base()" << std::endl;
        //     
        ::CloseHandle(m_hFile);
    }
};

try
{
    Base b;
}
catch(const std::exception &e)
{
    std::cout << "Exception message: " << e.what() << std::endl;
}

Output:
Hello from Base()
Exception message: Something failed


, . m_hFile ( ) .. CloseHandle() . .. : - .

: « ». : « ». :
class Base
{
private: 
    class CHandle
    {
    public:
        ~CHandle()
        {
            ::CloseHandle(m_handle);
        }
    private:
        HANDLE m_handle;
    public:
        //   smart pointer'   
        //  ,        
        void operator = (const HANDLE &handle)
        {
            m_handle = handle;
        }
    };

    CHandle m_hFile;

public:
    Base()
    {
        std::cout << "Hello from Base()" << std::endl;
        m_hFile = ::CreateFileA(...);
        //  ,       
        SomeLib.SomeFunc(...);
    }

    virtual ~Base()
    {
        std::cout << "Hello from ~Base()" << std::endl;
    }
...

Base , .. Base m_hFile CHandle, .

, , , , . boost, Loki, ATL .., .

3. const?

:
  1. , , ..
1. :
const int i = 1;
i = 2; // error C3892: 'i' : you cannot assign to a variable that is const

2. :
int i = 1;
int* const j(&i);
int k = 2;
*j = k; // Ok
j = &k; // error C3892: 'j' : you cannot assign to a variable that is const


3. :
class Foo
{
private:
    int i;
public:
    void func() const
    {
        i = 1; // error C3490: 'i' cannot be modified because it is being accessed through a const object
    }
};

: , mutable. mutable CPP .

4. - ?

: , , :
template <typename T >
void bubble_sort( T &a )
{
    for( T::size_type i = 0; a.size() && i < a.size() - 1; ++i )
    {
        for( T::size_type j = i; j + 1 > 0; --j )
        {
            if( a[j] > a[j+1] )
                std::swap( a[j], a[j+1] );
        }
    }
}

std::vector<int> v;
v.push_back( 7 );
v.push_back( 1000 );
v.push_back( 134 );
v.push_back( 23 );
v.push_back( 1 );
bubble_sort( v );

5. ?

: , , :
template <typename T >
void invert_string( T &a )
{
    T::size_type length = a.size();
    for( T::size_type i = 0; i < (length/2); ++i )
    {
        std::swap( a[i], a[length - i - 1] );
    }
}

std::string str = "abcdefg";
invert_string(str);

6. ?

: private =.
:
class NonCopyable
{
public:
    NonCopyable(){}

private:
    NonCopyable(NonCopyable&){}
    
private:
    void operator=(const NonCopyable&){}
};

NonCopyable a; 
NonCopyable b = a; // error C2248: 'NonCopyable::NonCopyable' : cannot access private member
a = b; // error C2248: 'NonCopyable::operator =' : cannot access private member

7. struct class?

: . struct public, class private. , structpublic, classprivate.
:
struct Foo
{
    int i;
};
class Bar
{
    int i;
};

Foo a;
a.i = 1; // Ok
Bar b;
b.i = 1; // error C2248: 'Bar::i' : cannot access private member declared in class 'Bar'

8. , ?

: .. , <, .
:
struct Foo
{
    int i;
};
inline bool operator < (const Foo & lhs, const Foo & rhs)
{
    return lhs.i < rhs.i;
}

std::set<Foo> data;
Foo a, b, c;
a.i = 7;
b.i = 1;
c.i = 6;
data.insert( a );
data.insert( b );
data.insert( c );
//   data     1 6 7

operator < Foo set, .. set'.

STL . , STL , , .

, (Comparison class), , .

9. ?

: sizeof + ( 4 ) + sizeof vtable ( ) + , ( * )
:
struct Foo
{
    int i;
    char a;
};

int size = sizeof(Foo); // 8 ,  int + char = 5.       4, ..       4 .

//    1 
#pragma pack(push, 1)
struct Foo
{
    int i;
    char a;
};
#pragma pack(pop)

int size = sizeof(Foo); // 5 

10. pure virtual function call ?

: .. , . .. , .
:
class Base
{
public:
    Base()
    {
        base_func();
    }
    void base_func()
    {
        func(); // pure virtual function call exception
    }
    virtual void func() = 0;
};
class Derived : public Base
{
public:
    virtual void func()
    {
    }
};

11. vector deque?

: deque push_front pop_front. , vector -, .. , deque . vector , deque / (O(1) O(n)), , .

12. ?

: .
:
class Base
{
public:
    int i;
};
class Derived : private Base
{
    // i     private
};
class Derived2 : private Derived
{
public:
    Derived2()
    {
        i = 2; // error C2247: 'Base::i' not accessible because 'Derived' uses 'private' to inherit from 'Base'
    }
};

Derived d;
d.i; // error C2247

private protected public private. protected public protected. public . .

13. ?

(Wiki): — , e ( ). () .

. : public.

. : private, protected.

14. ?

(Wiki): .
:
class Figure 
{
    ...
    void Draw() const;
    ...
};
 
class Square : public Figure 
{
    ...
    void Draw() const;
    ...
};
 
class Circle : public Figure 
{
    ...
    void Draw() const;
    ...
};


.. , , Draw().

15. malloc new?

: malloc — , (non-typesafe), .. void * . new, (typesafe), .. .

16. ?

: — , . — . , .. .
:
//  
class Foo
{
public:
    //   
    virtual void func() = 0;
};
class Bar : public Foo
{
public:
    virtual void func()
    {
    }
};

Foo f; // error C2259: 'Foo' : cannot instantiate abstract class	
Bar b; // Ok

17. throw ?

: .
:
try
{
    //....
    try
    {
        // Call something
    }
    catch(const std::exception& )
    {
        // Make/Check something..
        throw; //    
    }
    //...
}
catch(const std::exception& e)
{
    std::cout << e.what() << std::endl;
}


18. delete delete[]?

: delete , new(). delete[] new[]().
:
class Foo
{
};

Foo *pFoo = new Foo();
delete pFoo;
Foo *pFooArray = new Foo[10]();
delete[] pFoo;

delete (, delete delete[]) : undefined behavior.

19. auto_ptr?

: , . STL .
:
std::auto_ptr<Foo> a(new Foo);
std::auto_ptr<Foo> b;
b = a; // a     Foo

std::vector<std::auto_ptr<Foo>> v;
v.push_back(b); // error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available or copy constructor is declared 'explicit'

, auto_ptr delete, new[](). - boost , , , shared_ptr shared_array.

20. volatile?

: , , . , .
:
volatile int i = 1; //    ,     .

.. volatile - , , , , volatile .
:
while (1) 
{
    if(i == 1)
    {
        // -    i
    }
}

//   volatile ,        - :
if(i == 1) //    i  ,    ,    
{
    while (1) 
    {
        // -    i
    }
}


24.04 13.35

Source: https://habr.com/ru/post/J117996/


All Articles