в Smart указатель smart <int>o1 (new int ()) не хочет разрешать - PullRequest
0 голосов
/ 24 января 2020

В программе ниже, что происходит в этой строке smart<int> o1 = new int();?

Если я не хочу разрешать smart<int> o1 (new int() );, что мне делать?

Может ли кто-нибудь мне помочь? Я хотел бы лучше понять C ++, я пытался разными способами, например, перегрузить оператор присваивания, также используя явное ключевое слово.

#include <iostream>

using namespace std;
template <typename T>
class smart
{
    private:

     T *ptr;


    public:

    smart()
    {
        ptr = nullptr;
    }
     smart(T *ptr1)
    {
        cout<<"\nCalling Constructor *";
        ptr = ptr1;  
    }


     ~smart()
    {

        delete ptr; 
    }


    T *operator->()
    {
        cout<<"\nCalling ->";
        return ptr;
    }


    T &operator*()
    {
        cout<<"\nCalling *";
        return *ptr;
    }



};
int main()
{

    smart<int> o1 = new int() ; //What happen on this line
    smart<int> o1 (new int() ); //dont want to allow this syntax ?
    cout<<"\nHello World";

    return 0;
}
...