Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Equates or Synonyms?

Reply
Thread Tools

Equates or Synonyms?

 
 
Mike Copeland
Guest
Posts: n/a
 
      11-27-2011
Is there a C/C++ language capability to "equate" or use a synonym for
an identifier? I have the following declaration:

struct DEF_STRUCT
{
int EPace; // event maximum pace
int TFCount; // Total Finishers Counts
short ADepth; // Awards Depth
short SFMales, SFFemales; // Skipped Finishers
unsigned short dbeMales, dbeFemales;
unsigned short finMales, finFemales;
unsigned short prtMales, prtFemales;
float EDist; // event distance in miles
[etc.]
} ;

and I'd like to "reuse" some of the names (such as EPace or ADepth) with
different identifiers. That is, in some of my programs where I have
this structure declared I'd like to use a different name for, say,
EPace, because I'm not actually using that data element in that
particular application.
Other languages such as Pascal have an "equate" statement which does
this, and I can't find such a feature in C/C++. Does this exist? TIA
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-27-2011
On 11/28/11 12:07 PM, Mike Copeland wrote:
> Is there a C/C++ language capability to "equate" or use a synonym for
> an identifier? I have the following declaration:
>
> struct DEF_STRUCT
> {
> int EPace; // event maximum pace
> int TFCount; // Total Finishers Counts
> short ADepth; // Awards Depth
> short SFMales, SFFemales; // Skipped Finishers
> unsigned short dbeMales, dbeFemales;
> unsigned short finMales, finFemales;
> unsigned short prtMales, prtFemales;
> float EDist; // event distance in miles
> [etc.]
> } ;
>
> and I'd like to "reuse" some of the names (such as EPace or ADepth) with
> different identifiers. That is, in some of my programs where I have
> this structure declared I'd like to use a different name for, say,
> EPace, because I'm not actually using that data element in that
> particular application.


A different name for EPace, or a different type?

If the latter, use a typedef in place of int for the type.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-28-2011
On 28.11.2011 00:07, Mike Copeland wrote:
> Is there a C/C++ language


I think you mean C++, since you're posting in a C++ group.

There is no such language as "C/C++".

C and C++ are different languages.


> capability to "equate" or use a synonym for
> an identifier?


Yes, but it depends on the kind of identifier.

E.g. `typedef` works for types.

Deriving a class works for class types.

Function pointer works for function.

References work for v

> I have the following declaration:
>
> struct DEF_STRUCT
> {
> int EPace; // event maximum pace
> int TFCount; // Total Finishers Counts
> short ADepth; // Awards Depth
> short SFMales, SFFemales; // Skipped Finishers
> unsigned short dbeMales, dbeFemales;
> unsigned short finMales, finFemales;
> unsigned short prtMales, prtFemales;
> float EDist; // event distance in miles
> [etc.]
> } ;


Don't use ALL UPPERCASE identifiers except for macros, where you should
always use them (as a rule).

That's both to avoid readers becoming eyesore and deaf from all the
SHOUTING, and in order to have a separate set of possible names for
macros, which don't respect scopes.

Don't sweat it with saving some bits and bytes. The `short` elements
here are perhaps not particularly troublesome, but the `unsigned short`
elements are bug attractors, and the `float` is an unnecessary small
inefficiency on modern computers.

Also, don't try so hard to save on typing.

Remember, when Ken Thompson was asked what he'd do differently if he
were to design Unix from scratch again, he answered: "I'd spell creat
with an e".

Saving on single characters, with names like "creat" and "hlt", is just
dumb.

Also, use a /single convention/ for data member / variable names, like
starting with lowercase name, or starting with uppercase, but not a
haphazard alteration between different conventions.

So, like ...


struct ProperNameForThisTYpe
{
int eventMaximumPace; // was "EPace" shortening
int totalFinishersCount; // was "TFCount" shorting
int awardsDepth; // was "ADepth" of type short
int skippedFinishersMale; // was "SFMales" of type short
int skippedFinishersFemale; // was "SFemales" of type short
int dbeMales, dbeFemales; // what do these names mean?
int finMales, finFemales; // what do these names mean?
int prtMales, prtFemales; // what do these names mean?
double evenDistanceInMiles; // was "EDist" of type float
[etc.]
} ;



> and I'd like to "reuse" some of the names (such as EPace or ADepth) with
> different identifiers. That is, in some of my programs where I have
> this structure declared I'd like to use a different name for, say,
> EPace, because I'm not actually using that data element in that
> particular application.


Why do you want a different name when you're not using it?

The simplest solution is then to ignore the name.

After all, you're not using it.


> Other languages such as Pascal have an "equate" statement which does
> this,


No, you're perhaps thinking of Fortran.

> and I can't find such a feature in C/C++. Does this exist? TIA


See above.


Cheers & hth.,

- Alf
 
Reply With Quote
 
Mike Copeland
Guest
Posts: n/a
 
      11-28-2011
> > Is there a C/C++ language capability to "equate" or use a synonym for
> > an identifier? I have the following declaration:
> >
> > struct DEF_STRUCT
> > {
> > int EPace; // event maximum pace
> > int TFCount; // Total Finishers Counts
> > short ADepth; // Awards Depth
> > short SFMales, SFFemales; // Skipped Finishers
> > unsigned short dbeMales, dbeFemales;
> > unsigned short finMales, finFemales;
> > unsigned short prtMales, prtFemales;
> > float EDist; // event distance in miles
> > [etc.]
> > } ;
> >
> > and I'd like to "reuse" some of the names (such as EPace or ADepth) with
> > different identifiers. That is, in some of my programs where I have
> > this structure declared I'd like to use a different name for, say,
> > EPace, because I'm not actually using that data element in that
> > particular application.

>
> A different name for EPace, or a different type?
>
> If the latter, use a typedef in place of int for the type.


A different name - for personal documentation.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-28-2011
On 11/28/11 05:19 PM, Mike Copeland wrote:
>>> capability to "equate" or use a synonym for
>>> an identifier?

>>
>> Yes, but it depends on the kind of identifier.
>> References work for v

> ^^^^^^^^^^^^^^^^^^^^^
> I don't understand what you mean here.
>
>>> I have the following declaration:
>>> struct DEF_STRUCT
>>> {
>>> int EPace; // event maximum pace
>>> int TFCount; // Total Finishers Counts
>>> short ADepth; // Awards Depth
>>> short SFMales, SFFemales; // Skipped Finishers
>>> unsigned short dbeMales, dbeFemales;
>>> unsigned short finMales, finFemales;
>>> unsigned short prtMales, prtFemales;
>>> float EDist; // event distance in miles
>>> [etc.]
>>> } ;
>>> and I'd like to "reuse" some of the names (such as EPace or ADepth) with
>>> different identifiers. That is, in some of my programs where I have
>>> this structure declared I'd like to use a different name for, say,
>>> EPace, because I'm not actually using that data element in that
>>> particular application.

>
>> Why do you want a different name when you're not using it?
>> The simplest solution is then to ignore the name.
>> After all, you're not using it.

>
> I only want to use a more descriptive name for the variable in the
> application code. I want to maintain _some_ level of useful
> documentation in my code...


Then don't bother with the name of type, just give the variable a better
name. Try "eventMaximumPace" rather than "EPace". Also heed Alf's
advice about mixing naming convections!

--
Ian Collins
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      11-28-2011
Mike Copeland wrote:
> > > capability to "equate" or use a synonym for
> > > an identifier?


Alf P. Steinbach wrote:
> > Yes, but it depends on the kind of identifier.
> > References work for v


On 28 Nov., Mike Copeland wrote:
> ^^^^^^^^^^^^^^^^^^^^^
> I don't understand what you mean here.


He means something like this:
struct DEF_STRUCT
{
int EPace;
int& ABetterNameForEPace;
/* snip */

// This constructor for DEF_STRUCT binds
// ABetterNameForEPace to EPace.
DEF_STRUCT ()
: ABetterNameForEPace (EPace)
{}
};

Then you can do:

DEF_STRUCT s;
s.EPace = 5;
std::cout << s.ABetterNameForEPace << std::endl;
s.EPace = 7;
std::cout << s.ABetterNameForEPace << std::endl;

and you'll get the output
5
7

Note that introducing references may alter the binary layout of your
struct. Most probably the reference will be implemented through an
additional pointer.

There is a non-portable compiler extension for Microsoft Visual C++
which lets you achieve the same thing but without any changes to the
binary layout. Google for __declspec property.

However, the simplest would be to just add a setter and getter method
to the struct:
struct DEF_STRUCT
{
int EPace;
int getABetterNameForEPace ()
{
return EPace;
}
void setABetterNameForEPace (int NewValue)
{
EPace = NewValue;
}
/* snip */
};

Regards,
Stuart

PS: Please keep the names of the people you are quoting from. Better
yet, keep a complete history of the talk (at least the part that you
are refering to). Not everybody has access to all postings of this
group.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-28-2011
On Mon, 2011-11-28, Alf P. Steinbach wrote:
> On 28.11.2011 00:07, Mike Copeland wrote:

....
>> struct DEF_STRUCT
>> {
>> int EPace; // event maximum pace
>> int TFCount; // Total Finishers Counts
>> short ADepth; // Awards Depth
>> short SFMales, SFFemales; // Skipped Finishers
>> unsigned short dbeMales, dbeFemales;
>> unsigned short finMales, finFemales;
>> unsigned short prtMales, prtFemales;
>> float EDist; // event distance in miles
>> [etc.]
>> } ;

....

> Also, don't try so hard to save on typing.
>
> Remember, when Ken Thompson was asked what he'd do differently if he
> were to design Unix from scratch again, he answered: "I'd spell creat
> with an e".


(But I bet he wouldn't have called it createFile.)

Anyway, I think the struct above shows something else: an awkward name
is often a sign of deeper awkwardness. There's room for at least one
struct type here, with a "males" and a "females" instance.
That way you can say 'foo.males.finish' or whatever -- and documentation
becomes easier.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Mike Copeland
Guest
Posts: n/a
 
      11-28-2011
> >>> capability to "equate" or use a synonym for
> >>> an identifier?
> >>> I have the following declaration:
> >>> struct DEF_STRUCT
> >>> {
> >>> int EPace; // event maximum pace
> >>> int TFCount; // Total Finishers Counts
> >>> short ADepth; // Awards Depth
> >>> short SFMales, SFFemales; // Skipped Finishers
> >>> unsigned short dbeMales, dbeFemales;
> >>> unsigned short finMales, finFemales;
> >>> unsigned short prtMales, prtFemales;
> >>> float EDist; // event distance in miles
> >>> [etc.]
> >>> } ;
> >>> and I'd like to "reuse" some of the names (such as EPace or ADepth) with
> >>> different identifiers. That is, in some of my programs where I have
> >>> this structure declared I'd like to use a different name for, say,
> >>> EPace, because I'm not actually using that data element in that
> >>> particular application.

> >
> >> Why do you want a different name when you're not using it?
> >> The simplest solution is then to ignore the name.
> >> After all, you're not using it.

> >
> > I only want to use a more descriptive name for the variable in the
> > application code. I want to maintain _some_ level of useful
> > documentation in my code...

>
> Then don't bother with the name of type, just give the variable a better
> name. Try "eventMaximumPace" rather than "EPace". Also heed Alf's
> advice about mixing naming convections!


That's one reason why I want this capability: I know the name isn't
"good", but I have any applications where it's in my code but I want to
gradually migrate to the better name. Some of these applications are
old and don't need updates right now, so I'd leave them alone for a
while while I work on current stuff...
 
Reply With Quote
 
Mike Copeland
Guest
Posts: n/a
 
      11-28-2011
> > Is there a C/C++ language capability to "equate" or use a synonym for
> > an identifier? I have the following declaration:
> >
> > struct DEF_STRUCT
> > {
> > int EPace; // event maximum pace
> > int TFCount; // Total Finishers Counts
> > short ADepth; // Awards Depth
> > short SFMales, SFFemales; // Skipped Finishers
> > unsigned short dbeMales, dbeFemales;
> > unsigned short finMales, finFemales;
> > unsigned short prtMales, prtFemales;
> > float EDist; // event distance in miles
> > [etc.]
> > } ;
> >
> > and I'd like to "reuse" some of the names (such as EPace or ADepth) with
> > different identifiers. That is, in some of my programs where I have
> > this structure declared I'd like to use a different name for, say,
> > EPace, because I'm not actually using that data element in that
> > particular application.
> > Other languages such as Pascal have an "equate" statement which does
> > this, and I can't find such a feature in C/C++. Does this exist? TIA

>
> All instances of this struct will have the same members.
> The data members cannot have a different names across different instances,
> all instances have the same names.
>
> You could have 2 different structs with the same name in different
> namespaces, i.e:
>
> namespace A{
> struct def_struct{
> int dataNameA;
> };
> }
>
> namespace B{
> struct def_struct{
> int dataNameB;
> };
> }
>
> Then you just use using namespaceX for whatever one you want to use. Or you
> could play around using namespaces in different ways, but its not clear
> exactly what you are trying to do. References to data members may be another


That's not possible for my applications. The structure and object
are defined and declared in common library files, and there are many
applications that use them asis. The common library files establish and
populate this structure object, but in some newer applications I want to
use some of the data fields in other ways that they're more commonly
used. I know that the integrity of the data won't be compromised by
this reuse, because it's discrete within this application - the data
doesn't extend beyond it.
Thus, I want the structure object to be initialized (by common
processing), but I want to manipulate some of the data fields (with
better names) uniquely in some new programs. Specifically, I want to
use some of the "int" variables as counters for special application(s),
knowing that their normal usage isn't being applied in the new program
(s). Hence, I'd like to use better, more descriptive names there...
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-28-2011
On 11/29/11 12:03 PM, Mike Copeland wrote:
>>>>> capability to "equate" or use a synonym for
>>>>> an identifier?
>>>>> I have the following declaration:
>>>>> struct DEF_STRUCT
>>>>> {
>>>>> int EPace; // event maximum pace
>>>>> int TFCount; // Total Finishers Counts
>>>>> short ADepth; // Awards Depth
>>>>> short SFMales, SFFemales; // Skipped Finishers
>>>>> unsigned short dbeMales, dbeFemales;
>>>>> unsigned short finMales, finFemales;
>>>>> unsigned short prtMales, prtFemales;
>>>>> float EDist; // event distance in miles
>>>>> [etc.]
>>>>> } ;
>>>>> and I'd like to "reuse" some of the names (such as EPace or ADepth) with
>>>>> different identifiers. That is, in some of my programs where I have
>>>>> this structure declared I'd like to use a different name for, say,
>>>>> EPace, because I'm not actually using that data element in that
>>>>> particular application.
>>>
>>>> Why do you want a different name when you're not using it?
>>>> The simplest solution is then to ignore the name.
>>>> After all, you're not using it.
>>>
>>> I only want to use a more descriptive name for the variable in the
>>> application code. I want to maintain _some_ level of useful
>>> documentation in my code...

>>
>> Then don't bother with the name of type, just give the variable a better
>> name. Try "eventMaximumPace" rather than "EPace". Also heed Alf's
>> advice about mixing naming convections!

>
> That's one reason why I want this capability: I know the name isn't
> "good", but I have any applications where it's in my code but I want to
> gradually migrate to the better name. Some of these applications are
> old and don't need updates right now, so I'd leave them alone for a
> while while I work on current stuff...


Being somewhat lazy these days, I'd just load up the projects in my
current IDE and select refactor->rename. Job done.

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple equates jzakiya Python 27 12-04-2008 07:21 PM
Refilling your printer cartridges now equates to committing a crime? XP NZ Computing 8 09-17-2005 03:55 AM
Does Perl have anything that equates to the C++ this pointer? Calvin Perl 2 08-01-2004 08:55 PM



Advertisments