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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:39:11+00:00 2026-06-17T17:39:11+00:00

I stumbled upon this problem earlier today. I have this class and it contains

  • 0

I stumbled upon this “problem” earlier today.

I have this class and it contains a conversion operator. Something like:

class myClass {
public:
    ...

    operator anotherClass*() { return anotherClassPtr; }

    ...
}

Now this all works well .. until I made this silly mistake:

void yetAnotherClass::foo(myClass* _p_class) 
{
  ...

  anotherClass* lp_anotherClass = (anotherClass*)_p_class;

  ...
}

And it took me a long time to figure out why the lp_AnotherClass ptr was set to something non-zero whilst I was sure that the anotherClassPtr in _p_class was 0.

Is there anything I could have added to myClass that would have prevented me from making this mistake? (i.e. the compiler would have spat out some error) Is it possible to prevent an object ptr being cast to something else?

  • 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-17T17:39:12+00:00Added an answer on June 17, 2026 at 5:39 pm
    anotherClass* lp_anotherClass = (anotherClass*)_p_class;
    

    First of all, you should not use C-style cast. Use C++-style cast. That would have saved your time, as the compiler would tell you the problem immediately:

    auto* lp_anotherClass = static_cast<anotherClass*>(_p_class); //ERROR
    

    Secondly, prefer using explicit conversion function:

    explicit operator anotherClass*() { return anotherClassPtr; }
    

    Why I suggest explicit conversion function, because it avoids subtle bugs arises from implicit conversions, and additionally, it increases the code readability!

    Note that explicit conversion function is a C++11 feature.

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

Sidebar

Related Questions

I stumbled upon this issue: myObject 's class has a public field MyField which
I've just stumbled upon this problem on the net. It kind of goes something
I stumbled upon this problem and it took my a while to realise what
Today I stumbled upon this thread: http://www.mathworks.com/matlabcentral/newsreader/view_thread/112560 The question is basically how to make
I'm pretty new in iphone programming and have stumbled upon this issue which I
I'm doing some Delphi (2010) work this summer, and I've stumbled upon this problem:
The original problem I tried to solve when stumbled upon this was to select
I'm interested in social networks and have stumbled upon something which makes me curious.
I have stumbled upon the following problem. The below code snippet does not link
Im quite new to Java and i have stumbled upon the following problem. Im

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.