日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++中嵌套类的使用

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++中嵌套类的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一個類可以定義在另一個類的內部,前者稱為嵌套類(nested class)或嵌套類型(nested type)。嵌套類常用于定義作為實現部分的類。嵌套類可用于隱藏實現細節。

嵌套類是一個獨立的類,與外層類基本沒什么關系。特別是,外層類的對象和嵌套類的對象是相互獨立的。在嵌套類的對象中不包含任何外層類定義的成員;類似的,在外層類的對象中也不包含任何嵌套類定義的成員。

嵌套類的名字在外層類作用域中是可見的,在外層類作用域之外不可見。和其它嵌套的名字一樣,嵌套類的名字不會和別的作用域中的同一個名字沖突。

嵌套類中成員的種類與非嵌套類是一樣的。和其它類類似,嵌套類也使用訪問限定符來控制外界對其成員的訪問權限。外層類對嵌套類的成員沒有特殊的訪問權限,同樣,嵌套類對外層類的成員也沒有特殊的訪問權限。

嵌套類在其外層類中定義了一個類型成員。和其它成員類似,該類型的訪問權限由外層類決定。位于外層類public部分的嵌套類實際上定義了一種可以隨處訪問的類型;位于外層類protected部分的嵌套類定義的類型只能被外層類及其友元和派生類訪問;位于外層類private部分的嵌套類定義的類型只能被外層類的成員和友元訪問。

嵌套類必須聲明在類的內部,但是可以定義在類的內部或者外部。當我們在外層類之外定義一個嵌套類時,必須以外層類的名字限定嵌套類的名字。在嵌套類在其外層類之外完成真正的定義之前,它都是一個不完全類型。

嵌套類和外層類是相互獨立的:盡管嵌套類定義在其外層類的作用域中,但是外層類的對象和嵌套類的對象沒有任何關系。嵌套類的對象只包含嵌套類定義的成員;同樣,外層類的對象只包含外層類定義的成員,在外層類對象中不會有任何嵌套類的成員。

在C++11之前,嵌套類僅僅可以使用外層類的類型名、靜態成員和枚舉類型。但在C++11中,遵循非靜態成員的通用使用規則,嵌套類可以使用外層類的任何成員。

下面是從其他文章中copy的測試代碼,詳細內容介紹可以參考對應的reference:

#include "nested_class.hpp"
#include <iostream>
#include <vector>
#include <typeinfo>namespace nested_class_ {
//
// reference: http://en.cppreference.com/w/cpp/language/nested_types
int x, y; // globals
class enclose { // enclosing classint x; // note: private membersstatic int s;
public:struct inner { // nested classvoid f(int i) {//x = i; // Error: can't write to non-static enclose::x without instance//int a = sizeof(x); // Error until C++11,// OK in C++11: operand of sizeof is unevaluated,// this use of the non-static enclose::x is allowed.s = i;   // OK: can assign to the static enclose::s::nested_class_::x = i; // OK: can assign to global xy = i;   // OK: can assign to global y}void g(enclose* p, int i) {p->x = i; // OK: assign to enclose::x}};
};class enclose_ {struct nested { // private membervoid g() {}};
public:static nested f() { return nested{}; }
};int test_nested_class_1()
{//enclose_::nested n1 = enclose_::f(); // error: 'nested' is privateenclose_::f().g(); // OK: does not name 'nested'auto n2 = enclose_::f(); // OK: does not name 'nested'n2.g();return 0;
}// reference: http://www.sanfoundry.com/c-tutorials-nested-structure-access/
/* structure A declared */
typedef struct A {int a;float b;
} New_a;/* structure B declared */
typedef struct B {int c;float d;struct A e;    /* member 'e' is itself a structure */
} New_b;int test_nested_class_2()
{/* Let's declare variables of New_a and New_b */New_a bread;New_b butter;        /* 'butter' is a nested structure *//* Let's access bread using dot operator */bread.a = 10;        /* assigned member a value 10 */bread.b = 25.50;/* Let's access butter using dot operator */butter.c = 10;butter.d = 50.00;/* Let's access member 'e' which is a nested structure */butter.e.a = 20;butter.e.b = 20.00;/* Display values of members of 'butter.e' structure */printf("butter.e.a is %4d\n", butter.e.a);printf("butter.e.b is %.2f\n", butter.e.b);return 0;
}// reference: http://www.geeksforgeeks.org/nested-classes-in-c/
/* start of Enclosing class declaration */
class Enclosing {int x;/* start of Nested class declaration */class Nested {int y;void NestedFun(Enclosing *e) {std::cout << e->x;  // works fine: nested class can access// private members of Enclosing class}}; // declaration Nested class ends here
}; // declaration Enclosing class ends hereint test_nested_class_3()
{return 0;
}// reference: http://www.oopweb.com/CPP/Documents/CPPAnnotations/Volume/cplusplus16.html
class Clonable {
public:class Base {public:virtual ~Base() {}virtual Base *clone() const = 0;};private:Base *d_bp;public:Clonable() : d_bp(0) {}~Clonable() { delete d_bp; }Clonable(Clonable const &other) { copy(other); }Clonable &operator=(Clonable const &other){if (this != &other) {delete d_bp;copy(other);}return *this;}// New for virtual constructions:Clonable(Base const &bp){d_bp = bp.clone();      // allows initialization from}                           // Base and derived objectsBase &get() const{return *d_bp;}private:void copy(Clonable const &other){if ((d_bp = other.d_bp))d_bp = d_bp->clone();}
};class Derived1 : public Clonable::Base
{
public:~Derived1(){std::cout << "~Derived1() called\n";}virtual Clonable::Base *clone() const{return new Derived1(*this);}
};int test_nested_class_4()
{std::vector<Clonable> bv;bv.push_back(Derived1());std::cout << "==\n";std::cout << typeid(bv[0].get()).name() << std::endl;std::cout << "==\n";std::vector<Clonable> v2(bv);std::cout << typeid(v2[0].get()).name() << std::endl;std::cout << "==\n";return 0;
}/
// reference: http://www.sanfoundry.com/cpp-program-illustrate-nested-classes/
class Stack {class Node {public:int data;Node* next;Node(int data, Node* next);~Node();}*head;
public:Stack();Stack(const Stack& s);void operator=(const Stack& s);~Stack();void push(int data);int peek() const;int pop();
};Stack::Node::Node(int data, Node* next)
{this->data = data;this->next = next;
}Stack::Node::~Node() { }Stack::Stack() { head = NULL; }Stack::Stack(const Stack& s)
{head = s.head;
}void Stack::operator=(const Stack& s)
{head = s.head;
}void Stack::push(int data)
{head = new Node(data, head);
}int Stack::peek() const {if (head == 0) {std::cerr << "Stack empty!" << std::endl;return -1;}elsereturn head->data;
}int Stack::pop()
{if (head == NULL) return -1;int result = head->data;Node* oldNode = head;head = head->next;delete oldNode;return result;
}Stack::~Stack()
{if (head != NULL) {while (head->next != NULL) {Node* temp = head;head = head->next;delete temp;}}
}int test_nested_class_5()
{Stack Integers;int value, num;std::cout << "Enter the number of elements ";std::cin >> num;while (num > 0) {std::cin >> value;Integers.push(value);num--;}while ((value = Integers.pop()) != -1)std::cout << "Top element of stack  " << value << std::endl;return 0;
}//
// reference: https://msdn.microsoft.com/en-us/library/71dw8xzh.aspx
class X
{template <class T>struct Y {T m_t;Y(T t) : m_t(t) { }};Y<int> yInt;Y<char> yChar;public:X(int i, char c) : yInt(i), yChar(c) { }void print(){std::cout << yInt.m_t << " " << yChar.m_t << std::endl;}
};int test_nested_class_6()
{X x(1, 'a');x.print();return 0;
}///
// reference: https://msdn.microsoft.com/en-us/library/71dw8xzh.aspx
template <class T>
class X_
{template <class U>class Y {U* u;public:Y();U& Value();void print();~Y();};Y<int> y;
public:X_(T t) { y.Value() = t; }void print() { y.print(); }
};template <class T>
template <class U>
X_<T>::Y<U>::Y()
{std::cout << "X_<T>::Y<U>::Y()" << std::endl;u = new U();
}template <class T>
template <class U>
U& X_<T>::Y<U>::Value()
{return *u;
}template <class T>
template <class U>
void X_<T>::Y<U>::print()
{std::cout << this->Value() << std::endl;
}template <class T>
template <class U>
X_<T>::Y<U>::~Y()
{std::cout << "X_<T>::Y<U>::~Y()" << std::endl;delete u;
}int test_nested_class_7()
{X_<int>* xi = new X_<int>(10);X_<char>* xc = new X_<char>('c');xi->print();xc->print();delete xi;delete xc;return 0;
}} // namespace nested_class_

GitHub:?https://github.com/fengbingchun/Messy_Test?

總結

以上是生活随笔為你收集整理的C++中嵌套类的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。