We help IT Professionals succeed at work.

class conversions

ambitious
ambitious asked
on
Medium Priority
268 Views
Last Modified: 2010-04-02
hi all
i would like to know everything about class conversions i.e converting to and fro from the class and intrinsic data types with examples
i'll be very thankful
Ambitious:=)
Comment
Watch Question

>> i would like to know everything about class conversions
read Stroustrup's "The C++ Programming Language"

As a short overview, it's about this:

* implement conversion & assignment type T --> class Foo

class Foo
{
  protected:
    void Assign(T const & src) { ... /* fill your code */ }

  public:
    Foo(T const & source)
    {
      // .... init members if requried
      Assign(src);
    }

    Foo & operator=(T const & src)
    {
      Assign(src);
      return *this;
    }
};


* to implement a conversion Foo->T

class Foo
{
  operator T () { return ...  }
};


* conversion may implicitely be called

e.g.

void test(T x);

can be called with

Foo xfoo;
test(xfoo); // will call Foo.operator()

the other way round,

void test(Foo f);

T xt;
test(xt); // will create a temporary Foo instance

for exact rules when / when not, refer to standard or Stroustrup.

* to exclude a constructor from the implicit conversion, use the "explicit" keyword.

Peter

Commented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "peterchen" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner

Explore More ContentExplore courses, solutions, and other research materials related to this topic.