1

I'm new to C++ so I watch some Youtube Videos, I was watching a video about the arrow operator and also typing the code in the video

#include<iostream>
using namespace std;
 class Entity{
     public:
  void display(){

   cout<<"Inside Entity"<<endl;

 }

 };

 class Smatrptr{

     private:

 Entity* e;

 public:

 Smatrptr(Entity* entity)
 :e(entity)
 {
 }

 ~Smatrptr(){
 delete e;
 }

 Entity* getEntity(){
      return e;
 }

 };


int main(){


   Smatrptr smrt = new Entity();
   smrt.getEntity()->display();


return 0;
}


the code compile and run fine , what I don't understand is the line

Smatrptr smrt = new Entity();

as you can see smrt is of Type Smatrptr while new Entity is of type Entity* so how there is no "type mismatch" here?

  • 3
    `Smatrptr` has a constructor taking `Entity*`. `smrt` is initialized by calling that constructor. – Igor Tandetnik Mar 14 '21 at 00:50
  • 3
    "I watch some Youtube Videos" oh no no no no. Youtube programming tutorials aren't exactly the best quality, and I can already see one bad practice: `using namespace std;`. There's a list of C++ books compiled by SO members [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Also, "Smatrptr" should be "Smartptr" – SuperStormer Mar 14 '21 at 01:00
  • thanx Igor for the answer although i think im missing something really really basic i your answer but dont worry im dumb but ill figure it out – ayoub ifadir Mar 14 '21 at 01:12
  • SuperStormer thanks Smatrptr was a typo i didnt catch , i also know that "using namespace std;" is considered bad practice but im too lazy – ayoub ifadir Mar 14 '21 at 01:14
  • `Smatrptr smrt = new Entity();` is equivalent to the two lines `Entity* aux = new Entity();` `Smatrptr smrt(aux);` given that assignment ( the `=` operator) is treated as construction when used to define new objects. – Giogre Mar 14 '21 at 01:27
  • Note that your class `Smatrptr` violates rule of 3/5/0 https://en.cppreference.com/w/cpp/language/rule_of_three – Slava Mar 14 '21 at 03:01

0 Answers0