Equates or Synonyms?

M

Mike Copeland

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
 
I

Ian Collins

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

Alf P. Steinbach

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
 
M

Mike Copeland

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.
 
I

Ian Collins

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!
 
S

Stuart Redmann

^^^^^^^^^^^^^^^^^^^^^
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.
 
J

Jorgen Grahn

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
 
M

Mike Copeland

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...
 
M

Mike Copeland

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...
 
I

Ian Collins

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.
 
M

Mike Copeland

Being somewhat lazy these days, I'd just load up the projects in my
current IDE and select refactor->rename. Job done.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Huh? What is this? I've never seen or heard of any such "IDE
capability" (of course, I use VS6.0, which everyone constantly tells me
it sucks...). <sigh...>
 
I

Ian Collins

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Huh? What is this? I've never seen or heard of any such "IDE
capability" (of course, I use VS6.0, which everyone constantly tells me
it sucks...).<sigh...>


It's a feature of NetBeans and I assume Eclipse as well.

All copes of VS6.0 should have self destructed many, many years ago!
 
R

ralph

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Huh? What is this? I've never seen or heard of any such "IDE
capability" (of course, I use VS6.0, which everyone constantly tells me
it sucks...). <sigh...>

Visual Studio, out-of-the-box, only supports the Refactor! option for
VB.Net and C#. However, Microsoft offers a Refactor Plug-In for VC++
you can download at ...
http://msdn.microsoft.com/en-us/vstudio/bb737896

Worth a look.

-ralph
 
K

Kalle Olavi Niemitalo

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...

Could you use anonymous unions for that?

struct DEF_STRUCT
{
union
{
int EPace; // event maximum pace
int CounterForSpecialApplication;
};
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.]
} ;
 
J

Jorgen Grahn

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Huh? What is this? I've never seen or heard of any such "IDE
capability" (of course, I use VS6.0, which everyone constantly tells me
it sucks...). <sigh...>

In cases like this, it doesn't matter that your IDE sucks. If you
rename the definition of the struct member, the compiler will check
that you've renamed the uses correctly. A Perl one-liner or similar
can do the actual renaming.

The rest is a matter of management, e.g. being allowed to make trivial
changes to a stable application, being allowed to argue that these
changes are safe without a month of testing, and being able to postpone
delivery to users until some vital (to the users) change comes along.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top