How to initialize class template constructor in C++

1 Answer

0 votes
#include <iostream>

template<class T>
class Example {
public:
    T a, b;
    Example(T x, T y) : a(x), b(y) {}   // constructor
    
    void show() {
        std::cout << a << " " << b;
    }
};


int main() {
    int x = 10;
    int y = 25;
    
    Example<int> obj(x, y);
    
    obj.show(); 
}




/*
run:

10 25

*/

 



answered Dec 5, 2022 by avibootz

Related questions

1 answer 116 views
1 answer 150 views
1 answer 139 views
1 answer 153 views
1 answer 166 views
166 views asked Dec 10, 2020 by avibootz
1 answer 217 views
...