Question about overloaded operator

Posted on 2006-05-23
Last Modified: 2010-04-01
I have the following class

template <class T, type_t N>
class MyClass : public BaseClass
      typedef T valuetype;
       Constructor. Constructs a MyClass object and sets its value to x.
    inline MyClass (const T x = (T)0) : i_value(x) {}

    inline operator T () const
      { return i_value; }

where i_value is a member of BaseClass.
My question is: how can I use the "inline operator T ()" to access the i_value member of an element of a MyClass object?
Question by:Rothbard
    LVL 86

    Accepted Solution

    You can either apply it explicitly or have the compiler use it implicitly, e.g.

    Myclass<int,int> mc;

    int i = (int) mc; // the 'int' cast will invoke 'Myclass::operator T()' with 'T' being 'int'
    int j = mc; // the compiler knows that this assignment requires to invoke 'Myclass::operator T()' with 'T' being 'int'
    LVL 48

    Expert Comment

    I guess if T is int, you can write:
    int value = (int)myClassInstance;

    Author Comment

    Thanks a lot for your replies!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now