Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Example according to this Program

0 votes
asked May 17, 2020 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.

explanation:

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, 2020 by xDELLx (10,500 points)
//Need to compile with modern c++ compiler (I used c++17)

#include<iostream>

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;
    o.x=t;
    return o;
}

template<typename T>
class Object{
    private:
    T x;
    public:
    Object(){x=0;}
    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);
    obj.getX();
    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.
...