473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 2266
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.GetHashCode ();}
}

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(remove)@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
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)...
3
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()':...
3
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...
3
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...
9
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...
2
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
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>...
19
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
by: tonvandenheuvel | last post by:
Hi all, please consider the following piece of code: #include <iostream> template<typename T> class A { public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.