🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2882

Re: Stuff C++ needs

#2882
From: Barry Margolin
Date: Mon, 04 Dec 1995 16:30
62 lines
3295 bytes
In article <49r5vc$pk2@news.aloha.com>, Jimen Ching <jching@aloha.com> wrote:
]
]Hi,
]	Since we're on the subject, I have another feature I would like
]some opinions.  This feature is based on the following premise:
]
]	C++ allows the programmer to deal with user defined types like
]	any other types, including internal types.

This statement is incorrect.  There are a number of differences between the
primitive types and user-defined classes.  For instance, you can't use a
primitive type as a base class.  And there's no syntax for literals of
user-defined types.

]internal_type const a('a');
]internal_type const b('b');
]internal_type const c('c');
]
]f(internal_type &it)
]	{
]	switch (it)
]		{
]		case a: /* do something... */ break;
]		case b: /* do something... */ break;
]		default: /* do something... */ break;
]		}
]	}

The parameter to a case label is required to be an integer constant
expression.  The above code would be just as invalid if the type of "a" and
"b" were "const int" or if they were floating point literals.  This is not
an example of discrimination against user-defined types, since there are
many internal types that also can't be used there.

The switch statement is constrained like this because it's intended to be
compiled into a jump table.  The premise of C (and to a lesser extent, C++)
is that similar-looking statements should compile into similar code.

]If C++ lived up to its promise, why can't I do the above?  You might
]wonder where I got this idea.  Well, I was working on a project which
]needs to model register states, such as logic low, logic high, high
]impedance and don't care.  So I have a class called 'logic'.  But
]instead of using a switch for each logic state, I have to use a bunch
]of 'if', 'else if' statements.  This is not a problem for 4 states, but
]many digital circuits can test for 'strengths' in logic levels, and 
]with 'char' type, I can support upto 256 states.  Also, what if I
]wanted to do the same thing for a special sequence of states, such
]as 1,5,9,15,77,99.  I would have to test for each one of these.  Not
]a problem if the sequence is small, but what if it was over 50 values?
]I understand why C couldn't do this, but why can't C++?

How about using subclasses for the different states?  Then you can use a
virtual member function to implement the dispatch.
-- 
Barry Margolin
BBN PlaNET Corporation, Cambridge, MA
barmar@bbnplanet.com
Phone (617) 873-3126 - Fax (617) 873-6351

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <49tbaj$msb@tools.bbnplanet.com>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
References: <49lk0o$itd@engnews1.eng.sun.com> <49q17v$936@news.csus.edu> <49r5vc$pk2@news.aloha.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: Barry Margolin <barmar@tools.bbnplanet.com>
Newsgroups: comp.std.c++
Subject: Re: Stuff C++ needs
Date: 4 Dec 1995 16:30:19 GMT
Organization: BBN Planet Corp., Cambridge, MA
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <49tbaj$msb@tools.bbnplanet.com>
References: <49lk0o$itd@engnews1.eng.sun.com> <49q17v$936@news.csus.edu> <49r5vc$pk2@news.aloha.com>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
Content-Length: 2469
X-Lines: 58
Originator: clamage@taumet