How to create object on the heap in C++

1 Answer

0 votes
#include <iostream>
#include <string>
 
class Example {
private:
    int m_A;
    std::string m_Str;
public:
     
    Example() : m_A(0), m_Str("***") {}
     
    void Print() const {
        std::cout << m_A << " " << m_Str << "\n";
    }
};
  
 
int main() {
    Example* ex = new Example(); // new = heap
      
    ex->Print();
    
    delete ex; // delete the object
}
   
   
   
   
/*
run:
   
0 ***
   
*/

 



answered Feb 2, 2023 by avibootz

Related questions

1 answer 97 views
97 views asked Nov 21, 2022 by avibootz
1 answer 182 views
1 answer 115 views
1 answer 106 views
106 views asked Feb 2, 2023 by avibootz
1 answer 145 views
1 answer 205 views
...