Thread: comversion operator overloading

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    comversion operator overloading

    let's say I have a class:
    Code:
    template<class T>
    class Container
    {
      public:
        Container(const T& t) : m_T(t) {  }
    
        Container<T>& operator = (const T& t) { m_T = t; return *this; }
        operator T () { return m_T; }
    
      private:
        T m_T;
    };
    is there a way to make sure that the conversion operator returns a reference to the private data member? I have googled this subject and nobody even talks about this aspect of conversion operators. Is the default return type a copy or a reference?

  2. #2
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Shouldn't you supply both?
    goto( comeFrom() );

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't use conversion operators. They're Evil™.
    That said, a get function is useful here. To be efficient, they should return a const reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Don't use conversion operators. They're Evil™.
    That said, a get function is useful here. To be efficient, they should return a const reference.
    as unopposed as I'd normally be to having a get() function, I want to use a bigger version of that class to add the ability to any type to make it NULL (as in a database NULL). A conversion operator that returns a non-const reference would be an ideal solution to this, but it seems that it simply doesn't work that way.

    edit:

    or if they would add support for C#-style properties to the C++ standard
    Last edited by Elkvis; 06-04-2010 at 03:14 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't understand what you're trying to do, or how. You might want to elaborate on that and show some prototype code.
    But conversion operators probably isn't the answer. They do a lot of weird stuff to your code. Plus returning a non-const reference from a conversion operator is asking for trouble. It breaks encapsulation and is generally considered bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    I don't understand what you're trying to do, or how. You might want to elaborate on that and show some prototype code.
    your wish is my command

    Code:
    template<class T>
    class NullableColumnValue
    {
    	public:
    		NullableColumnValue() : m_isnull(true) {  }
    		NullableColumnValue(const T& t) : m_T(t), m_isnull(false) {  }
    		NullableColumnValue(const NullableColumnValue& rhs) : m_T(rhs.m_T), m_isnull(rhs.m_isnull) {  }
    		~NullableColumnValue() {  }
    		
    		void Value(const T& t) { m_T = t; m_isnull = false; }
    		void Value(const NullableColumnValue<T>& t) { m_T = t.m_t; m_isnull = t.m_isnull; }
    		T& Value(void) { if (!m_isnull) return m_T; else throw NullableColumnException("Column value is null."); }
    		const T& Value(void) const { if (m_isnull) throw NullableColumnException("Column value is null."); else return m_T; }
    		
    		bool IsNull(void) const { return m_isnull; }
    		
    		NullableColumnValue& operator = (const T& t) { m_T = t; m_isnull = false; return *this; }
    		NullableColumnValue& operator = (const NullableColumnValue<T>& t) { m_T = t.m_T; m_isnull = t.false; return *this; }
    		
    		bool operator == (const NullableColumnValue<T>& t)
    		{
    			if (t.m_isnull && m_isnull) return true;
    			if (t.m_isnull && !m_isnull) return false;
    			if (!t.m_isnull && m_isnull) return false;
    			return (t.m_T == m_T);
    		}
    		bool operator == (const T& t)
    		{
    			if (m_isnull) return false;
    			return (t == m_T);
    		}
    		
    		// the solution I'll probably end up using
    		T& operator () () { if (!m_isnull) return m_T; else throw NullableColumnException("Column value is null."); }
    		const T& operator () () const { if (!m_isnull) return m_T; else throw NullableColumnException("Column value is null."); }
    
    		// the original idea
    		operator T () { if (!m_isnull) return m_T; else throw NullableColumnException("Column value is null."); }
    		
    		operator bool (void) const { if (m_isnull) return false; else return true; }
    		
    		static NullableColumnValue<T> NullValue(void) { return NullableColumnValue<T>(); }
    		
    	private:
    		T m_T;
    		bool m_isnull;
    };

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, this doesn't explain why you need the conversion operator. And why is there a member Value AND an assignment operator?
    You need to answer why you simply cannot use a get function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    all of the (seemingly) superfluous functions are a result of quickly writing the code, and then going back and adding to it, after finding it to be inconvenient the first way. that's sort of my style of coding... at least while I'm developing something. I rarely strip out the unused bits until I'm actually done and ready to put it into regular use.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Elkvis: Note this simplification and improvement:
    Code:
    		bool operator == (const NullableColumnValue<T>& t) const
    		{
    			return (t.m_isnull == m_isnull) && (t.m_isnull || t.m_T == m_T);
    		}
    		bool operator == (const T& t) const
    		{
    			return !m_isnull && t == m_T;
    		}
    Learn to love short-circuit evaluation - half the number of null tests in the first one!


    You should also look up the safe-bool idiom.

    Don't waste time writing empty destructors either.
    Last edited by iMalc; 06-04-2010 at 10:24 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed