?
Solved

Question about overloaded operator

Posted on 2006-05-23
3
Medium Priority
?
222 Views
Last Modified: 2010-04-01
I have the following class

template <class T, type_t N>
class MyClass : public BaseClass
{
public:
      
      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?
Thanks.
0
Comment
Question by:Rothbard
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 16744845
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'
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16744847
I guess if T is int, you can write:
int value = (int)myClassInstance;
0
 

Author Comment

by:Rothbard
ID: 16784101
Thanks a lot for your replies!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

864 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