Example according to this Program

0 votes
asked May 17 by robin07 (120 points)
Language in C++

Write a program in c++ , in which make a template class Object and define global function , make_object which takes a reference to its templated type to bind to. This function will

be the friend function of Object class.


make a template class name" Object" . make global Function which is friend Function for Class. and proceed according to qUESTION

1 Answer

0 votes
answered May 31 by xDELLx (1,450 points)
//Need to compile with modern c++ compiler (I used c++17)


template<typename T>
class Object;

template<typename T,typename U=T>
Object<U> make_object( T t /*U&& u ,if want other type of Object ??*/){
    Object<T> o;
    return o;

template<typename T>
class Object{
    T x;
    void getX(){ std::cout<<x<<std::endl;};
    template <typename X,typename U>
    friend Object<U> make_object(X x);


int main (){
    //Object<int> o;
    int x=100;
    auto obj = make_object(x);
    return 0;
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.