3 Replies - 460 Views - Last Post: 28 June 2011 - 01:50 PM Rate Topic: -----

#1 daianahoney   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 11

Opposite behaviour from Conversion Constructor

Posted 28 June 2011 - 11:07 AM

hi everyone.
I know that you can use conversion constructor to assign a primitive type to a class:
class X
{ 
   int val;
   X(int x) { val=x;}
//...
}



but how do you do it when you need to have main() like this:
int main()
{
  INT i=10;
  int ii=i;
}


what do I need in the INT class?
Thanks in advance! Cheers!
Is This A Good Question/Topic? 0
  • +

Replies To: Opposite behaviour from Conversion Constructor

#2 daianahoney   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 11

Re: Opposite behaviour from Conversion Constructor

Posted 28 June 2011 - 11:46 AM

sorry, i lack some clarity
[code]
class X
{
int val;
public:
X(int x=0) { val=x;}
//...
}
void f()
{
X i=3;
}
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Opposite behaviour from Conversion Constructor

Posted 28 June 2011 - 11:49 AM

You need a conversion operator. In this case that would look like this (inside your class):

operator int() { return val; }
Was This Post Helpful? 0
  • +
  • -

#4 daianahoney   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 11

Re: Opposite behaviour from Conversion Constructor

Posted 28 June 2011 - 01:50 PM

thank you for your time!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1