久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 靜態模板成員,每個模板類型一個實例?

C++ static template member, one instance for each template type?(C++ 靜態模板成員,每個模板類型一個實例?)
本文介紹了C++ 靜態模板成員,每個模板類型一個實例?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

通常一個類的靜態成員/對象對于具有靜態成員/對象的類的每個實例都是相同的.無論如何,如果靜態對象是模板類的一部分并且還依賴于模板參數呢?例如,像這樣:

Usually static members/objects of one class are the same for each instance of the class having the static member/object. Anyways what about if the static object is part of a template class and also depends on the template argument? For example, like this:

template<class T>
class A{
public:
  static myObject<T> obj;
}

如果我將 A 的一個對象轉換為 int 并將另一個對象轉換為 float,我想會有兩個 obj,一個用于每種類型?

If I would cast one object of A as int and another one as float, I guess there would be two obj, one for each type?

如果我創建多個類型為 int 和多個 float 的 A 對象,它是否仍然是兩個 obj 實例,因為我我只使用兩種不同的類型?

If I would create multiple objects of A as type int and multiple floats, would it still be two obj instances, since I am only using two different types?

推薦答案

靜態成員對于每個不同的模板初始化都是不同的.這是因為每個模板初始化都是由編譯器在第一次遇到模板的特定初始化時生成的不同類.

Static members are different for each diffrent template initialization. This is because each template initialization is a different class that is generated by the compiler the first time it encounters that specific initialization of the template.

靜態成員變量不同的事實由這段代碼顯示:

The fact that static member variables are different is shown by this code:

#include <iostream>

template <class T> class Foo {
  public:
    static int bar;
};

template <class T>
int Foo<T>::bar;

int main(int argc, char* argv[]) {
  Foo<int>::bar = 1;
  Foo<char>::bar = 2;

  std::cout << Foo<int>::bar  << "," << Foo<char>::bar;
}

結果

1,2

這篇關于C++ 靜態模板成員,每個模板類型一個實例?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 欧美精品一区在线发布 | 日韩日韩日韩日韩日韩日韩日韩 | 一区二区免费在线观看 | 91精品国产91久久久久福利 | 日本午夜精品一区二区三区 | 综合色在线 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 在线免费观看黄色网址 | 久久精品色欧美aⅴ一区二区 | 日韩一二三 | 日韩一区精品 | 一区二区三区在线 | 欧美精三区欧美精三区 | 91精品国产综合久久久久久漫画 | 97超碰在线免费 | a黄在线观看 | 欧美精品在线观看 | 成人av网站在线观看 | 欧美在线| 黄色片免费看视频 | 97日日碰人人模人人澡分享吧 | av久久 | 一区二区三区视频在线观看 | 成人免费视频网站在线观看 | 天天狠狠| 一区二区三区国产 | 天天看天天操 | 国产午夜久久久 | 午夜爽爽爽男女免费观看 | 在线黄色影院 | 日本精品一区二区三区视频 | 久久综合一区 | 国产成人精品网站 | 国产在线一级片 | 一区在线播放 | 中文字幕在线电影观看 | 亚洲欧美成人在线 | 久久综合一区二区三区 | 日韩中文在线 | 国产乱码精品一区二三赶尸艳谈 | 欧美一级全黄 |