Transparent Class Wrapper

Discussion in 'C++' started by mike, Nov 23, 2005.

  1. mike

    mike Guest

    Ok,
    IO want to write a class to wrap around another class, to print to the
    screen when the vale is changed. So far I have:


    template<class T>
    class Tracer
    {
    T data;
    public:

    Tracer()
    {
    cout << "New Tracer:" << T() <<"\n";
    data = T();
    }

    Tracer(const T& t)
    {
    cout << "New Tracer:" << t<<"\n";
    data = t;
    }

    T& operator=(const T& t)
    {
    cout << "TracerChanged: " << t<<"\n";
    data = t;
    }




    };


    allowing me to write:
    Tracer<int> a;
    Tracer<int> b = 6;
    b = 12;
    a=4;

    but i can't go the other way around:
    int e = a; //Error

    Now i seem to remeber an explicit operator, and I googled for it this
    morning but no luck??
    Any thoughts would be apprieciated!

    Mike
    mike, Nov 23, 2005
    #1
    1. Advertising

  2. "mike" <> schrieb im Newsbeitrag
    news:...
    > Ok,
    > IO want to write a class to wrap around another class, to print to
    > the
    > screen when the vale is changed. So far I have:
    >
    >
    > template<class T>
    > class Tracer
    > {
    > T data;
    > public:
    >
    > Tracer()
    > {
    > cout << "New Tracer:" << T() <<"\n";
    > data = T();
    > }
    >
    > Tracer(const T& t)
    > {
    > cout << "New Tracer:" << t<<"\n";
    > data = t;
    > }
    >
    > T& operator=(const T& t)
    > {
    > cout << "TracerChanged: " << t<<"\n";
    > data = t;
    > }
    >
    >
    >
    >
    > };
    >
    >
    > allowing me to write:
    > Tracer<int> a;
    > Tracer<int> b = 6;
    > b = 12;
    > a=4;
    >
    > but i can't go the other way around:
    > int e = a; //Error
    >
    > Now i seem to remeber an explicit operator, and I googled for it
    > this
    > morning but no luck??
    > Any thoughts would be apprieciated!



    operator T() {return data;}

    HTH,
    -Gernot
    Gernot Frisch, Nov 23, 2005
    #2
    1. Advertising

  3. mike

    Mike Wahler Guest

    "mike" <> wrote in message
    news:...
    > Ok,
    > IO want to write a class to wrap around another class, to print to the
    > screen when the vale is changed. So far I have:
    >
    >


    #include <iostream>
    using std::cout;

    > template<class T>
    > class Tracer
    > {
    > T data;
    > public:
    >
    > Tracer()
    > {
    > cout << "New Tracer:" << T() <<"\n";
    > data = T();
    > }


    Tracer() : data(T())
    {
    cout << "New Tracer:" << data << '\n';
    }

    >
    > Tracer(const T& t)
    > {
    > cout << "New Tracer:" << t<<"\n";
    > data = t;
    > }


    Tracer(const T& t) : data(t)
    {
    cout << "New Tracer:" << t << '\n';
    }
    >
    > T& operator=(const T& t)
    > {
    > cout << "TracerChanged: " << t<<"\n";
    > data = t;


    return data = t;

    > }
    >


    operator T() const
    {
    return data;
    }

    >
    >
    >
    > };
    >
    >
    > allowing me to write:


    int main()
    {

    > Tracer<int> a;
    > Tracer<int> b = 6;
    > b = 12;
    > a=4;
    >
    > but i can't go the other way around:
    > int e = a; //Error


    return 0;
    }

    >
    > Now i seem to remeber an explicit operator, and I googled for it this
    > morning but no luck??
    > Any thoughts would be apprieciated!


    What you were looking for is 'conversion operator'.

    -Mike
    Mike Wahler, Nov 23, 2005
    #3
  4. mike

    mike Guest

    Thanks, thats the ticket!
    Mike
    mike, Nov 23, 2005
    #4
  5. "mike" <> schrieb im Newsbeitrag
    news:...
    > Thanks, thats the ticket!
    > Mike
    >


    Ooops! Forgot the "const" at the end:
    operator T() const {return data;}

    otherwise you might get stange errors some day...
    Gernot Frisch, Nov 23, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brendan Duffy
    Replies:
    0
    Views:
    3,986
    Brendan Duffy
    Jul 25, 2003
  2. John Mullin
    Replies:
    0
    Views:
    1,720
    John Mullin
    Nov 7, 2004
  3. Julian
    Replies:
    1
    Views:
    691
    Roland
    May 24, 2005
  4. E11
    Replies:
    1
    Views:
    4,708
    Thomas Weidenfeller
    Oct 12, 2005
  5. a
    Replies:
    3
    Views:
    355
    Axter
    Mar 15, 2006
Loading...

Share This Page