473,804 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implicit operator bool - question 1

How come I can write code like "if (L)" but NOT code like "if (L ==
true)" when L is a class with an implicit operator bool?

///////////

List L = new List();

public class List
{
private long count = 0;

public static implicit operator bool (List L)
{
return L.count > 0;
}
}

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 15 '05 #1
2 2284
Nov 15 '05 #2
the Code "If (L == true) reqires that L have an == operator.
Becase It does not, (L == true) does not evaluate to a function.
There are (at least) two ways to fix this
One way is to add the opperator overload to the List class
So it looks like the following
public class List
{
private long count = 0;
public static implicit operator bool (List L)
{return (L.count > 0);}
public static bool operator ==(List L, bool comparType)
{return (L.count > 0);}
public static bool operator !=(List L, bool comparType)
{return (L.count <= 0);}
public override bool Equals(object obj)
{return base.Equals (obj);}
public override int GetHashCode()
{return base.GetHashCod e ();}
}

The other way is to type cast it to a bool
if(((bool)L) == true)
this way the bool's operator == function is called.

Hope this helps

Andy Renk

Junker_Mail(rem ove)@yahoo.com -- delete "(remove)" to email

How come I can write code like "if (L)" but NOT code like "if (L ==
true)" when L is a class with an implicit operator bool?

///////////

List L = new List();

public class List
{
private long count = 0;

public static implicit operator bool (List L)
{
return L.count > 0;
}
}

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4334
by: Simon Ford | last post by:
Hi All, I'm having trouble understanding exactly how I can do some specific implicit casting. There are two problems here; does anyone know what I should be doing? //---------- // (1) Resolving as bool for comparison, but not as int. // // we have a class
3
1660
by: Boris | last post by:
Hello, I have written a program that compiles just fine with GCC 3.3. With GCC 3.4.1 it results in this error: g++ -O0 -g3 -Wall -c -oTestp.o ../Testp.c .../Testp.c: In function `int main()': .../Testp.c:26: error: no match for 'operator<=' in 't <= 2.0e+0' Please help me, I have no idea where the problem is.
3
2961
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the following error messages: warning: implicit typename is deprecated, please see the documentation for details Can someone kindly suggest how to get rid of these warnings? The source code follows.
3
1989
by: Jon Shemitz | last post by:
Why would you ever want to define both "public static bool operator true" and "public static bool operator false" when you can just define "public static implicit operator bool"? Is there syntax where the two are not equivalent? Or are there cases where you might legitimately want both false and true to return the same value? Fwiw, I'm having a hard time coming up with any C# 1.1 code that calls true or false operators. I'm inclined...
9
2090
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and emulate the pattern im trying to follow in an existing project. These are my steps: 1) I have a method "printMe" existing in the application which originally used to take in a string. This method is static and sits in the Driver
2
1916
by: Calum Grant | last post by:
Here's a little utility I thought I'd share. (For those familiar with ATL::CComQIPtr, the concept is similar). ==== #ifndef DYNPTR_H_INCLUDED #define DYNPTR_H_INCLUDED
3
4624
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string> #include <iostream> #include <cstring> int main(){
19
3907
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
0
1273
by: tonvandenheuvel | last post by:
Hi all, please consider the following piece of code: #include <iostream> template<typename T> class A { public:
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.