Overload the class itself

Assume i have a class:

class foo
{
    ....
    //some constructors here set val=34
    ....
    private:
    int val;
    int & foo::operator,()
    {
      return val;
    }
};

and i want to be able to use it as:

foo bar;
printf("  %d ", bar);   //i need to get value of bar.val
                        //with just using bar itself with
                        //using an overloading
                        //but , overloading does not work
                        //i need to get 34 without using bar.val
                        //i need this without any getter 
                        //i need val to be private

Question: Is this kind of overloading possible? If yes, how?

I tried:

int foo::operator int()
{
    return val;
}

but it says "return type may not be specifiet on a conversion function" :(

I tried:

operator int() const { return val; } 

conversion works only outside of printf & cout.

 int e=foo;
 printf(" %d ",e); //works 

Answers


You cannot overload a class, but you can use a conversion operator so that it will automatically convert to different types according to expected parameters in that context. In your case, as val is an int, you'd overload the conversion to int like this:

operator int() const { return val; }

Now wherever an int is required, and you provide a foo, this conversion is applied.

There are some limits. For example, if you pass foo to a function template where the type of the corresponding argument is generic, there will be no conversion in that place. The conversion also won't affect other expressions which don't enforce a type. As an example, if f is type foo, then &f will always be of type foo*, not int*. For most practical applications, all this is exactly what you need. But I believe that the C-style variadic printf is just such a case where there is no well-defined expected type. Better use a C++-style call, or an explicit cast static_cast<int>(f).

If neither is acceptable for you, then you are into trouble: there is no way that the C++ logic can deduce the fact that you require an int here, simply because you included a %d in some string constants. The type conversion is a compile-time thing, whereas the interpretation of format strings is done at runtime (except when generating warnings the way e.g. gcc does). The compiler won't know what type that argument needs to be, therefor it won't know what conversion to execute, therefore you have to help him in some way.

As long as this int val is the only member of foo, and there are no virtual functions either in this class, and neithe ris there a base class with any data members or a virtula function, the memory layout of an object of class foo will usually be the same as that of a plain integer. This means that the untyped C-style printf won't be able to tell the difference, even without any conversion at all. But relying on this is really bad style, so I'd advise against making use of this.


Need Your Help

rollback changes created by minitest unit test

ruby minitest

I'm writing a test case in Minitest that creates a database entry. After the test is run, all changes that the test did should be rolled back. What is a good way to achieve this?

VS2015 ASP.NET MVC the name 'model' does not exist in the current context

asp.net-mvc razor nuget visual-studio-2015

I was working on an ASP.NET MVC 4 in VS2013 and everything was fine, then I've uninstalled 2013 and installed 2015, I was able to open the project, build it and even run, everything works fine

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.