473,394 Members | 1,845 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,394 software developers and data experts.

Displaying two different data types with operator ?

How do I use two different data types with a conditional operator ?

I want to cout either a character or an integer depending on a
certain condition.

cout << ((IsThisTrue? char:integer)

The compiler seems to treat the char as an integer. The program
prints out the decimal value of the char.
Oct 9 '05 #1
1 2055
John Smith wrote:
How do I use two different data types with a conditional operator ?

I want to cout either a character or an integer depending on a
certain condition.

cout << ((IsThisTrue? char:integer)
You can't use the name char as an identifier.
The compiler seems to treat the char as an integer. The program
prints out the decimal value of the char.


An instance of the ternary conditional operator ?: constitutes a single
C++ expression. C++ is statically typed, and every expression can have
only one manifest type, which is determined at compile time as a
synthesized attribute from the types of its constituent expressions,
regardless of context.

In the case of the ternary expression, the type is determined from the
the second and third operands, which must either have the same type, or
it must be possible to convert them to a common type using the usual
arithmetic or pointer conversions. See the C++ standard for the exact
rules.

In the case of char and int, char value will be promoted to int, and
the overall expression will return type int no matter which way the
condition falls.

And that means that the ostream::operator << (int) will be called on
the cout object regardless of the value of isThisTrue.

The ?: operator cannot dynamically select between two different
overloads of the << operator for int and char; it offers a single type
to overload resolution.

Your intent can be expressed as :

if (isThisTrue)
cout << character;
else
cout << integer;

You could design a programming language in which the conditional
operator will do that by itself, but C++ isn't it.

Oct 9 '05 #2

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

Similar topics

2
by: Mitch Mooney | last post by:
Subject line says it all. For example: //base class class foo{ public: foo(); ~foo(); virtual ??? GetValue()=0; };
8
by: Jon Weston | last post by:
I'm setting up an Access2003 database with pictures. I put a bound ole picture ctrl on a form that's source is the table that contains the pictures and follow ALL the directions for embedding a...
7
by: Stephan Rose | last post by:
I am currently working on an EDA app and heavily working on squeezing the last bits of performance out of it. Going as far as sending batches of geometry to the video card while still processing...
7
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
2
by: chike_oji | last post by:
Hello, I am displaying data retrieved from a database as a httpresponse in a webform. I noticed that some characters such as the comma (,), display as special characters in the web browser...
4
by: rlwebbnafex | last post by:
Hello, I have never used generic programming so I need a little help for an idea on what I can do. I have defined a lot of classes like the following. The classes are all pretty much the same,...
5
by: ednaswap | last post by:
Hello, C++ experts, I am writing a Log class something like below. ================= c++ source code ======================== class Log { public: Log(); Log(string _filename);
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...

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.