gpt4 book ai didi

C++ 语法 : ClassA : private vector {} meaning

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:57 25 4
gpt4 key购买 nike

我正在读取 C++ 文件。在 C++ 中有一些我不理解的结构,但我不知道如何通过 google 来学习这个。这是我正在阅读的一段代码:

template <typename value_type>
class iterstack: private vector<value_type> {
private:
using vector<value_type>::crbegin;
using vector<value_type>::crend;
using vector<value_type>::push_back;
using vector<value_type>::pop_back;
using vector<value_type>::back;
typedef typename vector<value_type>::const_reverse_iterator
const_iterator;
public:
using vector<value_type>::clear;
using vector<value_type>::empty;
using vector<value_type>::size;
const_iterator begin() { return crbegin(); }
const_iterator end() { return crend(); }
void push (const value_type& value) { push_back (value); }
void pop() { pop_back(); }
const value_type& top() const { return back(); }
};

在上面的代码中,有些地方我不明白。

首先:

class iterstack: private vector<value_type>

上面的行与class iterstack: vector<value_type>有什么区别? (没有 private 关键字)

其次:

using vector<value_type>::crbegin;

我真的不明白这一行。 using关键字,我经常在这种形式下使用:using namespace std另一种使用方式让我感到困惑。

请告诉我上面两点的意思。

谢谢:)

最佳答案

privateclass iterstack: private vector<value_type>意味着 vector<value_type>是私有(private)基类。默认情况下,类的基类是私有(private)的,因此在这种情况下添加的 private 关键字无效。如果基类是私有(private)的,则其公共(public)和 protected 接口(interface)将作为私有(private)函数等添加到派生类中。

class Base {
public:
void fun(){}
};
class Derived1 : public Base { }; //fun() is public member function
class Derived2 : Base{}; //fun() is private
class Derived3 : private Base {}; //fun is private
class Derived4 : private Base {
public:
using Base::fun; //fun is public
}

using vector<value_type>::crbegin;公共(public)成员函数crbeginiterstack 中是私有(private)的(因为它来自用 private 关键字继承的基类)被制成 public .

关于C++ 语法 : ClassA : private vector<T> {} meaning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23167338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com