Thread: Conversion operators

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Conversion operators

    Hi all

    I need some explanation on understanding the concept of conversion operators.

    I read about conversion operators and found that they automatically convert one object into another one.

    Here is some code.

    Code:
    class A {
    ifstream file;
    string fileName;
    
    public:
    
    A(const string& fname)
    : fileName(fname), file(fname.c_str()) {}
    
    string name() const { return fileName; }
    
    void name(const string& newName) {
    fileName = newName;
    }
    
    operator ifstream&() { return file; } 
    };
    
    int main() {
    
    A a("A.cpp");
    cout << a.name() << endl;
    
    a.close(); // Compile time error. close() not member of A
    
    }
    I am little bit confused in this.
    Since automatic type conversion, converts object "a" to type of ifstream then it should be able to call close() function of ifstream. So why does it giving error.


    I will very thankful if some one answered my following question:

    1) HOW conversion operator work's, I mean to say how calls are gone to conversion operator?

    2) What is the criteria to convert one object to another, is this rule is universal?

    I have read some where about conversion operator:


    Automatic type conversion happens only in function calls, not during member selection
    What does it mean????

    Thanks for your time.

    Thanks
    Nickman

    Can
    Last edited by nickman; 06-08-2010 at 07:55 AM. Reason: Change some content

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your big misconception is that is somehow magically transforms the object from one type to another. That is simply not true.
    It creates a new object, then transforms this into the specified type and returns it.
    The cout statement calls the ifstream conversion operator which simply returns a reference to its member file. So the transformed object is then returned from the conversion operator and passed to cout.

    There are several things you should be aware of, though:
    Conversion operators should be avoided. They can cause a lot of implicit conversion that causes headaches.
    Returning a non-reference to a private member variable is usually considered bad because it breaks encapsulation.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks for reply....

    So that's mean that now "a" is object of type ifstream. So why it's now able to call close() function of ifstream?

    Thanks once again.

    Nickman

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nickman View Post
    So that's mean that now "a" is object of type ifstream. So why it's now able to call close() function of ifstream?
    Like I said, no, a is NOT an ifstream object. It is and always will be an A. Conversion operators does not transform the instance. It returns a transformed object.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Hi

    I am still not getting.

    It returns a transformed object.
    What is the transformed object?

    Could you please eloborate more on this????


    Thanks
    Nickman

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Taking your original code,
    Code:
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    class A 
    {
    private:
    	ifstream file;
    	string fileName;
    
    public:
    	A(const string& fname)
    		: fileName(fname), file(fname.c_str()) {}
    
    	string name() const { return fileName; }
    
    	void name(const string& newName) 
    	{
    		fileName = newName;
    	}
    
    	operator ifstream&() { return file; } 
    };
    
    int main() 
    {
    	A a("A.cpp");
    	ifstream & transformed_obj = a;
    	cout << a.name() << endl;
    	transformed_obj.close();
    }
    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.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Hi

    So what is object "a" is here?
    ifstream & transformed_obj = a;
    An instance of class A or transformed object as you are using it as initializer for transformed_obj .

    Thanks
    Nickman

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is only one a, and it's an instance of class A.
    The compiler then calls the conversion operator in A which returns an ifstream object which is assigned to transformed_obj.
    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.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    I got it...... Elysia thanks for your time....

    I had played with your code and found out that if comment out line:

    When I comment out line:

    Code:
    operator ifstream&() { return file; }
    Then compiler is giving me error that cannot convert instance of A to ifstream.

    It means that conversion operator is called only when the compiler see that(Suppose) in this expression there is requirement of other object type and THERE IS CONVERSION OPERATOR FUNCTION WHICH CAN RETURN THE DESIRABLE OBJECT.

    Also when I comment out the same line in my original code it was not giving any error( because no conversion was required at that time).

    Please correct me if wrong.

    Thanks a lot for your help.!!!!!!!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are correct. Now keep in mind what I mentioned earlier about conversion operators and references.
    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.

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks Elysia.... I'll keep in my mind

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Let me put it differently.
    Code:
    struct convertible {
      int get() const { return 0; }
      operator int() const { return 0; }
    };
    There isn't much difference between get() and the conversion operator. The only difference, really, is that the latter has a weird name and is called implicitly under some circumstances, while the former must be called explicitly. Other than that, they're the same.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overload the..bool operator..?
    By 39ster in forum C++ Programming
    Replies: 18
    Last Post: 11-26-2008, 06:24 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM