It's not working to put arguments through "...



  • Number N and sequence a1, a2, ... aN
    Create a class template that generates dynamic single-dimensional masses with different types of elements (natural, whole, symbolic, etc.). The type of data and result are parameters in relation to the class, the programme shall have initialization methods, designer, designer.

    template < int size, typename type > class A {
    

    type *a;

    public:

    A() {

    a = new type[size];

    }

    ~A() {

    delete[] a;
    

    }

    void init(type t...) {

    // пока просто хочу вывести эл-ты

    type *p_t = &t;

    for(int i = 0; i < size; i++)
    std::cout << *(p_t + i) << " ";

    }

    // перенести в *a проблем нет
    // проблемы происходят при чтении *(p_t + i)

    };

    when int all rules, float or double is the first element of the rules, and the rest of the debris.

    Compiler GNU GCC

    I can't create a type-type mass outside the classroom.
    It may, of course, be possible to create a type add(int index, type t), but it seems impossible to do so in the task (the initialization method).



  • Three points (allipsis) in the signature can only be used through macroes from <cstdarg> (sighs)va_list etc.).

    You can't write. void init(type t, ...) and think (&t + 1) Give the address to the second argument. Such an operation leads to uncertain behaviour (UB).

    Right.

    void init(type t, ...) {
        va_list args;
        va_start(args, t);
        std::cout << t << ' '; // первый параметр
        for (int i = 1; i < size; ++i) {
            std::cout << va_arg(args, type) << ' '; // остальные
        }
        va_end(args);
    }
    

    Use ... Not recommended. In this case, a designer is to be used. std::initializer_list<T>


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2