Sign in to follow this  

Automatic type conversion

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well how does this automatic type conversion work operator ifstream&(){return file} class FName1 { ifstream file; string fileName; bool named; ........................ } main { FName1 fileName(.....); fileName.close()//doesn't work here //but it works in other member functions (file.close()); } You're probably sick of me ,but i realy need help ThankX!

Share this post


Link to post
Share on other sites
You will not get automatic type conversion from an object to one of its members, unless you ask for it in some way.

Directly asking for the actual type conversion is probably a bad idea, but here are two ways to do it. Say you have classes Foo and Bar, and want to convert Foo to Bar implicitly:


class Bar {
// First way: 1-arg constructor in the target class.
public:
Bar(Foo & f) {
// construct a Bar from a Foo.
}
// If you need a constructor for Bar which takes a Foo but you
// *don't* want it to be used for implicit type conversion,
// put the keyword 'explicit' before the declaration.

// In your case you don't have this option, because 'Bar' is
// ifstream, and you're not going to touch that. (RIGHT?)
}

class Foo {
Bar myBar;
// Second way: operator overload in the source class.
// DO NOT DO BOTH! It will result in an ambiguity.
// (Although keyword explicit will resolve that.)

// This is what you were thinking of using I think; and I'm
// not at all sure of the syntax here, nor of exactly where
// you ought to use references vs. values.
Bar& operator Bar() {
return myBar;
}
}




However, either of these is probably a very bad idea.

Edit: And as twanvl was trying to say, it won't even work for calling a method on, or asking for a member from, the Foo. The language won't implicitly convert the thing you're trying to call a method on, because it doesn't know what to look for to try and convert it to. But given the object type and the method name, now it has enough information to go look up those methods and see what sets of arguments are possible, and then figure out what conversions need to be done in order to match one of those signatures.

What you probably want to do is make use of some kind of polymorphism. In a lot of these cases, a Foo is not really logically a kind of Bar, so you just want to let the Foo be a wrapper for the Bar functionality. This can be done by delegation:


class Bar {
public:
// Some method that Bars have.
void serveDrink(Customer &c) { // Sorry. :)
// ...
}
}

class Foo {
Bar myBar;
// Now we give Foo a serveDrink() method explictly, and just
// have it use the Bar method.
void serveDrink(Customer &c) {
myBar.serveDrink(c);
}
}




On the other hand, sometimes your Foo logically *is* a kind of Bar, in which case you should be using inheritance:


class Foo : public Bar {
// Even if serveDrink is not virtual in Bar, we can use it
// with a Foo object - we just won't get to change the
// behaviour unless we know at compile time that we have a
// Foo object specifically. (That is, the compiler checks
// at compile time to figure out which method to use; see
// below.) Also note that we no longer include the Bar as
// a data member of Foo.
}

Customer c;
Foo f;
f.serveDrink(c); // ok
Bar b = f;
b.serveDrink(c); // sort-of ok; if we write an implementation
// of Foo::serveDrink(), and the Bar declaration is not virtual,
// then Bar::serveDrink() is called and our implementation is
// ignored. I'm told that there are situations where you want
// this behaviour, but I've never been able to think of one
// myself.


Share this post


Link to post
Share on other sites

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this