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 6570865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:48:35+00:00 2026-05-25T14:48:35+00:00

How do I make a class in C++, when initialized, return a Boolean value

  • 0

How do I make a class in C++, when initialized, return a Boolean value when its name is invoked, but no explicit function call make, like ifstream. I want to be able to do this:

objdef anobj();
if(anobj){
  //initialize check is true
}else{
  //cannot use object right now
}

not just for initialization, but a check for its ability to be used.

  • 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-05-25T14:48:36+00:00Added an answer on May 25, 2026 at 2:48 pm

    The way istream does it is by providing an implicit conversion to void*

    • http://www.cplusplus.com/reference/iostream/ios/operator_voidpt/
    • stream output and implicit void* cast operator function invocation

    Update In reaction to the comments, the Safe Bool Idiom would be a far better solution to this: (code directly taken from that page)

      class Testable {
        bool ok_;
        typedef void (Testable::*bool_type)() const;
        void this_type_does_not_support_comparisons() const {}
      public:
        explicit Testable(bool b=true):ok_(b) {}
    
        operator bool_type() const {
          return ok_==true ? 
            &Testable::this_type_does_not_support_comparisons : 0;
        }
      };
    
      template <typename T> 
        bool operator!=(const Testable& lhs,const T& rhs) {
        lhs.this_type_does_not_support_comparisons();   
          return false; 
        } 
      template <typename T>
        bool operator==(const Testable& lhs,const T& rhs) {
        lhs.this_type_does_not_support_comparisons();
          return false;     
        }
    

    The article by Bjorn Karlsson contains a reusable implementation for the Safe Bool Idiom


    Old sample:

    For enjoyment, I still show the straight forward implementation with operator void* overloading, for clarity and also to show the problem with that:

    #include <iostream>
    
    struct myclass
    {
         bool m_isOk;
    
         myclass() : m_isOk(true) { }
         operator void* () const { return (void*) (m_isOk? 0x1 : 0x0); }
    };
    
    myclass instance;
    
    int main()
    {
        if (instance)
            std::cout << "Ok" << std::endl;
    
        // the trouble with this:
        delete instance; // no compile error !
        return 0;
    }
    

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

Sidebar

Related Questions

To make a JavaScript class with a public method I'd do something like: function
If we make any class in VB.NET and try to access its property in
I would like to make a child class that has a method of the
I need to make a 2D convex hull function for a class assignment and
In Python is there any way to make a class, then make a second
In Java (And in general) is there a way to make a class so
I want to make a generic class that accepts only serializable classes, can it
Is it acceptable to make more than 1 facade class (not instance) in a
In designing a class for customer for example would it make sense to use
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage

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.