3

I want to create a constructor, that can get an unknown number of ints so if someone wants to create a new MyObj(1,2) it should work, aswell as new MyObj(1,2,3,2,5) or new MyObj(1,2,3,2,..,n) .

Thank you

2 Answers2

6

There's a number of ways to achieve that (including the good old var_args as proposed as duplicate). One of the simplest is using a std::initializer_list:

class MyClass {
    std::vector<int> v;
public:
    MyClass(std::initializer_list init) : v(init) {}
};
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thank you ! Also, is there a way to get an uknown number of variables, of uknon type ? – John Traveli Dec 15 '16 at 17:58
  • @JohnTraveli I don't get your question? You need something like a `std::vector`?? – πάντα ῥεῖ Dec 15 '16 at 17:58
  • I assume that is what I want. – John Traveli Dec 15 '16 at 18:02
  • I want the constructor to be able to get for example 5 ints or five ints one string and one boolean . – John Traveli Dec 15 '16 at 18:03
  • any is not defined, compiler throws error – John Traveli Dec 15 '16 at 18:05
  • @JohnTraveli You should notice that [`std::any`](http://en.cppreference.com/w/cpp/utility/any) is a c++17 standard feature. Alternatively you can use [`boost::any`](http://www.boost.org/doc/libs/1_61_0/doc/html/any.html) for compilers that do not support the c++17 standard yet. – πάντα ῥεῖ Dec 15 '16 at 18:09
  • should I include a library for boost to work because its still throwing an error, I am using the latest version of xCode if this is any help to you , again thank you very much for your time! – John Traveli Dec 15 '16 at 18:16
  • @JohnTraveli In case you use boost, you should install it before of course. – πάντα ῥεῖ Dec 15 '16 at 18:19
  • Is this the only way to create a vector of unknown type of variables ? - its not that i dont want to use boost its that my partners dont have it. – John Traveli Dec 15 '16 at 18:27
  • @JohnTraveli Maybe some trickery with `std::tuple` but that needs at least all the type information resolved at compile time. – πάντα ῥεῖ Dec 15 '16 at 18:30
  • @JohnTraveli Extending that question for non uniform types of a _variables list_ is another (and very different) question. Consider asking another one instead of discussing that topic here in comments. You were primarily asking for the _varying number of variables_, not _varying types_ at the same time. – πάντα ῥεῖ Dec 15 '16 at 18:54
  • Thank you @πάντα ῥεῖ , also before I wave goodbye, the whole correct saying is " τά πάντα ῥεῖ ". I assume you are not Greek but the correct saying is τά πάντα ῥεῖ . Only with the τά can you have the correct meaning. – John Traveli Dec 15 '16 at 19:01
  • @JohnTraveli I'm preferring the shortcut (same translation at google at least). Also changing the nick it would disturb some kinda meme I've being setup so far. – πάντα ῥεῖ Dec 15 '16 at 19:04
  • Im Greek believe me when I say its not the same, and from what I understand you know Ancient greeks wouldnt say something if it was unnecessary, but never the less its true that you have a reputation with that name so changing it would be problematic – John Traveli Dec 15 '16 at 19:20
  • @John _"you know Ancient greeks wouldnt say something if it was unnecessary"_ You _hit the nail_. That's part of the philosophy ;) – πάντα ῥεῖ Dec 15 '16 at 19:24
  • Dont let your memes, be dreams :) – John Traveli Dec 15 '16 at 19:39
  • 1
    @JohnTraveli: ["πάντα ῥεῖ" was good enough for Plato](https://en.wikipedia.org/wiki/Heraclitus#Panta_rhei.2C_.22everything_flows.22). – Lightness Races in Orbit Dec 15 '16 at 20:21
  • @Lightness He was inventing _memes_ at their times may be ;) – πάντα ῥεῖ Dec 15 '16 at 20:23
  • @πάντα ῥεῖ when you are too deep in the dankmemes – John Traveli Dec 15 '16 at 20:25
  • @Lightness Races in Orbit I think it is good enough for anyone 1 – John Traveli Dec 15 '16 at 20:26
  • @John It's more my personal philosophy rather than a _meme_. I'm not hiding under rocks or so. It's not that I'm not acknowledged here and people miss to read my profile. – πάντα ῥεῖ Dec 15 '16 at 20:28
3

Yeah, you can use std::initializer_list so long as the types are all the same.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125