簡體   English   中英

從 Variadic Function 傳遞給構造函數的參數聲明和初始化 std::tuple

[英]Declaring and Initialising std::tuple from Variadic Function Parameters Passed to Constructor

我正在嘗試創建一個 std::tuple 的幾個大對象,我可以隨后使用,理想情況下訪問它們的方法並從 TextureManager 中做其他事情


class TextureManager
{
  public:
  template<typename T , typename... Args>
  TextureManager(T t, Args... args)
                :graphical_objects{std::make_tuple(t, args...)}
  {}

// How to declare graphical_objects ?

  std::tuple< /*what to put here*/  > graphical_objects;
};

int main()
{
  TextureManager tm1(1, 2, 'a');

// This is how I'd like to use TextureManager
  TextureManager text_manager2(my_graphics_obj1, my_graphics_obj2, my_graphics_obj3); 
// Or how ever many parameters...

 return 0;
}

這是 my_graphics_obj1 的玩具示例


class GraphicsObject
{
  virtual void CreateTextures() = 0;
};

class StringTexture: public GraphicsObject
{
  SDL_Texture* CreateTextures (/*params*/)
  {
    // do rendering and what not
    return A_SDL_Texture*;
  }
};

理想情況下,我能夠使用元組 graphical_objects 訪問 CreateTextures()。 這可能嗎?
我用谷歌搜索了我能想到的“成員”、“元組”、“可變參數模板”、“從中提取元素”和“聲明使用”的幾乎所有可能的單詞組合。 我很確定這不是這個問題或這個問題我不會鏈接到我看過的所有帖子,以節省所有相關時間。 我覺得答案很簡單,或者我完全錯了,而且涉及更多。

我確實可以訪問 C++17,目前我正在使用 clang 進行編譯(以獲得更好的錯誤消息),但那是在 C++14 上。 兩者/其中一個的解決方案都可以。

我要為自己爭取早期的年度最佳面部護理

以下現在在 C++17 中按預期工作。
感謝讓我找到解決方案的各種評論。
我還以為我在大約 4 小時前將#include<vector>更改為<tuple> 結果我沒有。


#include <iostream>
#include <tuple>

class GraphicsObject
{
  virtual void CreateTextures() = 0;
};

class MandelbrotTexture: public GraphicsObject
{
  float mtf = 78.4;
  void CreateTextures (/*params*/)
  {
    std::cout<<"returning SDL_Texture or GTexure\n";
    return;
  }
};

// template declaration should be here, not where it was...
template<typename T, typename... Args>
class TextureManager
{
  public:

// template<typename T, typename... Args>  this was the wrong place !

  TextureManager(T t, Args... args)
                :graphical_objects(std::make_tuple(t, args...))  // No {} !
  {}

// I'd also forgotten 'T,' and was just trying 'Args...', also wrong.

  std::tuple<T, Args...> graphical_objects;
};

int main(int argc, char const *argv[])
{
  MandelbrotTexture mt;
  TextureManager test(1, 2, 'a');
  TextureManager test2(mt, mt, mt, mt);
  return 0;
}

graphical_objects現在讓我可以訪問傳遞給構造函數的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM