Conversion of objects

Hi, I am trying to convert type of class object, please about some tips.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>

using namespace std;
class Base
{
public:
    int a;
    Base()
    {
        a = 0;
    }
    Base(int a)
    {
        this->a = a;
    }
    ~Base()
    {

    }
};
class Inherit_Class : public Base
{
public:
    double b;
    Inherit_Class()
    {
      b = 0;
    }
    Inherit_Class(double b)
    {
        this->b = b;
    }
    ~Inherit_Class()
    {

    }
};
class Class
{
public:
    int x;
    double y;
    Class()
    {
        x = 0;
        y = 0;
    }
    Class(int x,double y)
    {
        this->x = x;
        this->y = y;
        cout<<x<<endl;
        cout<<y<<endl;

    }
    ~Class()
    {

    }
    operator Base()
    {
        return Base();
    }
};
int main()
{
  Class object(2,3.5);
    return 0;
}
Last edited on
What exactly do you mean by “convert”? Do you mean like cast? There’s a cast operator if that’s what you mean


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class DeceptiveInt {
  int m_data;

  public:

  DeceptiveInt(int x): m_data(x) {}

  bool operator bool () {
    return !m_data;
  }
};


int main() {
  DeceptiveInt x = 7383;
  cout << bool(x); // output should be 0
  return 0;
}
Last edited on
Topic archived. No new replies allowed.