Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:10:56+00:00 2026-06-10T23:10:56+00:00

Assume i have a class: class foo { …. //some constructors here set val=34

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:10:57+00:00Added an answer on June 10, 2026 at 11:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just assume I have some class Foo, that has two dependencies: an ISerializer<T> and
Let's say I have the following code: public class Foo { private int x;
Assume we have following pseudocode: class XY { int X { get; set; }
Lets say I have business class: public class Foo { public int Prop1 {get;set;}
Let's assume we have a template function foo: template<class T> void foo(T arg) {
assume we have : class Dictionary { int n; int *ints; char **strs; inline
Assume the following: we have class B, which is a private class nested inside
Assume I have this: class foo{ Member member; foo(); ~foo(); }; How I should
Assume I have such simple model: class Foo(models.Model): name = models.CharField(max_length=25) b_date = models.DateField()
Let's assume we have following models: from django.db import models class Foo(models.Model): name =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.