How to use with class constructor with implicit conversion in C++

2 Answers

0 votes
#include <iostream>

class Example {
private:
    int m_A;
public:
     
    Example(int a) : m_A(a) {}
    
    void Print() const {
        std::cout << m_A << "\n";
    }
};
  
 
int main() {
    Example ex = 98; // Implicit Conversion // Constrct Example with 98
      
    ex.Print();
}
   
   
   
   
/*
run:
   
98
   
*/


 



answered Feb 2, 2023 by avibootz
0 votes
#include <iostream>

class Example {
private:
    int m_A;
public:
     
    Example(int a) : m_A(a) {}
    int getA() const {
        return m_A;
    }
};
  
    
void Print(const Example& ex) { // Constrct Example with 98
    std::cout << ex.getA() << "\n";
}
 
int main() {
    Print(98); // Implicit Conversion 
}
   
   
   
   
/*
run:
   
98
   
*/

 



answered Feb 2, 2023 by avibootz

Related questions

1 answer 113 views
113 views asked Feb 2, 2023 by avibootz
1 answer 109 views
2 answers 142 views
142 views asked Jan 29, 2023 by avibootz
1 answer 167 views
1 answer 128 views
...