Is it possible to overload a class for passing to a function?

I want to be able to do
 
someFunction(MyObject)

rather than
 
someFunction(MyObject.getWhatIWant())

I know I can do &MyObject, MyObject() and MyObject[0] by overloading operators but is it possible with just plain old MyObject?

this is assuming that I have no access to someFunction(), i.e, it cannot be modified.
Last edited on
Hi

Does MyObject.getWhatIWant() return the same type as Myobject?

If it doesn't, u can overload the someFunction
Your post leaves a lot unanswered, namely:

What type does the someFunction function take in it's parameters?
What type does MyObject.getWhatIWant() return?
Is MyObject a variable name or a type?
What do you mean by "...having no access to someFunction"?
Yes.

operator MyObject() const;

This gives the compiler a lot of easily abused superpowers, so you typically don't want to do it this way. It is better to be explicit (using the named member function) or to overload the function itself as instructed for you.

Hope this helps.
I have no access to someFunction(), i.e, it cannot be modified
OK it cannot be modified but you can do something like:
-define another function
1
2
3
4
some_other_function(MyClass MyObject)
{
    return someFunction(MyObject.getWhatIWant());
}

[edit]You can also make it inline so it won't be calling another function[/edit]
Last edited on
@Duoas

I did try overloading operator(), as I mentioned in the OP, but doing
someFunction(myClass()); is different to someFunction(myClass);

@JewelCpp

I would have to create lots of different kinds of functions for it to be seamless, and even so any user-defined functions would not be affected, so it's not a solution for me.

Here's a better explanation:

someFunction is
void someFunction(RawClass&);

WrapperClass.get() is
RawClass& WrapperClass::get() {return m_rawClassObject;}

At the moment, I'm doing this:
someFunction(WrapperClass.get());

what I want is this:
someFunction(WrapperClass);

without modifying someFunction to overload it (it's not practical)
Last edited on
Why is it not practical? It looks to be the most correct way to solve the issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// wrapperclass.hpp

#include "wrappedclass"

namespace baz
{

  class WrapperClass
  {
    ...
  };

  inline void someFunction( WrapperClass& wc )
  {
    someFunction( wc.get() );
  }

}

BTW, I did not suggest you overload operator(). Read carefully.

Hope this helps.
Ah sorry Duoas, I'll check again when I'm at a computer.

It's not practical because I need it to work with many functions, my own and of libraries.
Topic archived. No new replies allowed.