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

  • SEARCH
  • Home
  • 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 7248147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:05:58+00:00 2026-05-28T22:05:58+00:00

Horstmann’s C++ pitfalls tackles an interesting point when talking about streams. To quote him:

  • 0

Horstmann’s C++ pitfalls tackles an interesting point when talking about streams. To quote him:

Use conversion to void*, not conversion to int or bool, to implement objects yielding truth values. Unlike int or bool, void* have no legal operations other than == comparison.

As a programmer, I would be puzzled if some function returned void* when I expect a boolean. Horstmann provides an example where using a void* instead of a bool seems appropriate. Is it always advisable?

  • 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-28T22:05:58+00:00Added an answer on May 28, 2026 at 10:05 pm

    This is not advised in general circumstances and, with C++11, is not advised at all.

    The reason for the conversion to void* was to support syntax like

    std::ifstream myStream;
    if (myStream) {
    
    }
    if (!myStream) {
    
    }
    

    Here, a conversion to bool seems more reasonable, but leads to weirdnesses like this:

    if (myStream == true) // ??
    

    The conversion to void* prevents this code from being legal, but opens up a whole other can of worms, like

    delete myStream; // ??
    

    In C++11, with the ability to have explicit operator bool() as a member function, this void* hack is deprecated and should not be used. Don’t use this idiom. If you need something to return a bool, have it return a bool. If you need an object that can be converted to a bool, use explicit operator bool.

    Hope this helps!

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

Sidebar

Related Questions

I'm doing Cay Horstmann's combinator parser exercises, I wonder about the best way to
I was wondering how I could go about accessing values within an object, such
Here are two solutions to exercise 4.9 in Cay Horstmann's Scala for the Impatient:
I'm going through Java For Everyone by Cay Horstmann. I'm a bit confused on
From Cay Horstmann's C++ For Everyone Chapter 4: Loops Write a program that adds
I've got a task to create a webservice for school. The problem is for
I'm unable to figure how the standard (or just popular ) brace style names
EDIT: Thanks everyone. I didn't even notice it was private lol, so I changed
I'm a student starting out on my first programming module. The textbook I'm working
Morning, I'm having some trouble creating tables/loading fixtures. (symfony 1.4.6 with the bundled Doctrine

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.