回 帖 发 新 帖 刷新版面

主题:C++面试八股文:C++中,设计一个类要注意哪些东西?

某日二师兄参加XXX科技公司的C++工程师开发岗位第9面:

面试官:C++中,设计一个类要注意哪些东西?

二师兄:设计一个类主要考虑以下几个方面:1.面向对象的封装、继承及多态。2.big three或者big five。3.运算符和函数重载、静态成员、友元、异常处理等相关问题。

面试官:请介绍一下面向对象的三个特性。

二师兄:封装是将类的函数和数据封装起来,外部不能直接访问类的数据,而是需要通过方法访问数据。继承是指一个类可以继承另一个类的属性和方法。多态是指一个对象可以表现出多种形态。

面试官:请问多态是如何实现的?

二师兄:多态的是通过父类的指针或引用指向子类的对象实现的。在对象中维护一个虚指针(vtptr),这个指针指向一个虚表(vtable),当用户通过父类对象访问子类的方法时,通过查询虚表中对应的方法的地址,并跳转到此地址执行间接访问对象的方法。所以多态是有一点点运行时开销的。

面试官:你刚才所说的big threebig five是什么?

二师兄:(嘿嘿,被装到了)类的big three分别是拷贝构造函数(copy constructor)、拷贝赋值运算符(copy assignment)和析构函数。而类的big five则多了两个,分别是移动构造函数(move constructor)和移动赋值运算符(move assignment)。后面两个是C++11之后引入的。

面试官:好的。那你知道为什么要引入移动构造和移动赋值吗?

二师兄:主要是为了效率。移动构造和移动赋值不需要把所有的数据重新拷贝一遍,而是霸占了被移动对象的数据的所有权。代价是被移动对象在被移动后不能使用。

面试官:嗯。那你知道为什么移动构造和移动赋值都要加上noexcept关键字吗?

二师兄:额。。。好像不让抛异常?

面试官:你知道类的静态成员变量需要注意哪些问题吗?

二师兄:要注意哪些问题?额。。。

面试官:在成员方法后加const是为什么?

二师兄:主要是为了约束这个成员方法不更改对象的任何数据。

面试官:还有其他的原因吗?

二师兄:好像没有了吧。。。

面试官:类的成员方法可以通过const符号重载吗?

二师兄:这个,,应该可以吧。。

面试官:你知道什么是类的成员方法的引用限定符吗?

二师兄:没有听说过耶。。。

面试官:好的,回去等通知吧。

让我们来看一看今日二师兄的表现吧,

为什么移动构造和移动赋值都要加上noexcept关键字?

因为在使用移动语义时,通常会将资源的所有权从一个对象转移到另一个对象,而不是复制资源。如果抛出异常,那么在转移资源的过程中可能会出现问题,导致资源泄漏或其他不可预测的行为。

另外,加上 noexcept 关键字还可以提高代码的性能,因为编译器可以在不必要的情况下进行优化。

类的静态成员变量需要注意哪些问题?

静态成员变量的初始化顺序是不确定的。如果一个静态成员变量依赖于另一个静态成员变量的值,要确保第二个静态化成员先被初始化,否则程序可能会出现未定义的行为。

静态成员变量的值可以被多个实例同时修改,因此在多线程访问静态成员时要注意数据竞争问题。静态变量的生命周期与程序的生命周期相同,因此它们可能会占用大量的内存。

在成员方法后加const是为什么?

一是可以约束此方法不会更改对象的任何数据。二是cosnt对象也可以访问此成员方法。

#include <iostream>
struct Foo
{
    void f1(){std::cout <<"f1" << std::endl;}
    void f2() const{std::cout <<"f2" << std::endl;}
};
int main(int argc, char const *argv[])
{
    Foo foo;
    foo.f1();
    foo.f2();
    const Foo& foo2 = foo;
    foo2.f1();  //这里无法通过编译,因为const对象无法访问非const 方法
    foo2.f2();  //这里可以通过编译
}

类的成员方法可以通过const符号重载吗?

这是一个很好的问题,估计很多人没有思考过。先说答案,底层const可以,而顶层const不可以。

#include <iostream>
struct Foo{};
struct Goo
{
    void f1(Foo& f){std::cout <<"non const function" << std::endl;}
    void f1(const Foo& f){std::cout <<"const function" << std::endl;}
};
int main(int argc, char const *argv[])
{
    Foo foo;
    Goo goo;
    goo.f1(foo);    //无法通过编译,error: ‘void Goo::f1(Foo)’ cannot be overloaded with ‘void Goo::f1(Foo)’
    return 0;
}

当我们把顶层const改为底层const

#include <iostream>
struct Foo{};
struct Goo
{
    void f1(Foo& f){std::cout <<"non const function" << std::endl;}
    void f1(const Foo& f){std::cout <<"const function" << std::endl;}
};
int main(int argc, char const *argv[])
{
    Foo non_const_foo;
    const Foo const_foo;
    Goo goo;
    goo.f1(non_const_foo);    //可以通过编译	non const function
    goo.f1(const_foo);    //可以通过编译 const function
    return 0;
}

那么我们能否通过在函数括号后加上const来重载函数呢?

#include <iostream>
struct Goo
{
    void f1() {std::cout <<"non const function" << std::endl;}
    void f1() const{std::cout <<"const function" << std::endl;}
};
int main(int argc, char const *argv[])
{
    Goo non_const_goo;
    const Goo const_goo;
    non_const_goo.f1();   
    const_goo.f1();   
    return 0;
}

答案是肯定的,因为const_goo.f1() 可以等同于f1(const Goo* goo),也是底层const

最后一个问题虽然简单,但我相信至少有80%的C++程序员不知道是什么,

什么是类的成员方法的引用限定符吗?

类的成员方法的引用限定符是 C++11 中引入的一种新特性,用于指定成员方法的参数是左值引用还是右值引用。

#include <iostream>
struct Foo
{
    void f1() & {std::cout << "only left reference can call this function" << std::endl;}
    void f1() && {std::cout << "only right reference can call this function" << std::endl;}
};
int main(int argc, char const *argv[])
{
    Foo foo;
    foo.f1();       //left reference
    Foo().f1();     //right reference
    return 0;
}

好了,今日份面试到这里就结束了,小伙伴们,对于今天二师兄的面试,能打几分呢?如果是你,以上的问题都能回答的上来吗?



<顺便吆喝一句,民族企业大厂,前后端测试捞人,感兴趣的来!https://jsj.top/f/o38ijj >

回复列表 (共1个回复)

沙发

Treating surface damage on the flat chested sex doll requires patience and meticulous work. Shallow scratches can be gradually smoothed out with silicone polish, while deeper damage requires filling with specialized filler and then smoothing. When repairing a life size sex doll, ensure the overall skin texture is consistent, and the newly repaired area should match the texture of the surrounding skin. For the flat areas typically found on flat chested sex dolls, special care must be taken to maintain the natural curvature of the surface during repair to avoid unnatural flatness or bulges. It's recommended to compare colors under natural light, using small amounts of pigment to gradually adjust the color until the new and old materials blend completely. After each operation, allow the material to fully cure for 24 hours before proceeding.

我来回复

您尚未登录,请登录后再回复。点此登录或注册