How to use alias within a structure? 
Author Message
 How to use alias within a structure?

I want alias a variable within a structure, ie. an integer refered to as
hieght or weight depending on the context. I'm having touble with the
syntax.

typedef struct
{
        int     weight;
        int     &height = weight;   // alway gets syntax error here
        ......

Quote:
}

my_data_type;

Strangely enough, I have no problem with aliasing a single variable such
as:
        int     iv;
        int     &rv = iv;

Please help me out by kindly showing me some examples. Thanks.

PS. I try not to use any overloading if possible.


      [ about comp.lang.c++.moderated. First time posters: do this! ]



Wed, 06 Sep 2000 03:00:00 GMT  
 How to use alias within a structure?



Quote:
> I want alias a variable within a structure, ie. an integer refered to
as
> hieght or weight depending on the context. I'm having touble with the
> syntax.

> typedef struct
> {
>    int     weight;
>    int     &height = weight;   // alway gets syntax error here
>    ......
> }
> my_data_type;

> Strangely enough, I have no problem with aliasing a single variable
such
> as:
>    int     iv;
>    int     &rv = iv;

> Please help me out by kindly showing me some examples. Thanks.

> PS. I try not to use any overloading if possible.

<Jack>

You can't do this at all in C, even though you crossposted to
comp.lang.c, because there is no such thing as a reference and "int
&height" is just a syntax error.  Assuming that you defined it as "int
*height" leads to the following:

You can't assign a pointer variable with the address of weight, because
weight is not an object, it is a member of a my_data_type struct.  Even
if you have an instance of a my_data_type struct, weight is not an
object, but struct_name.weight is or struct_ptr->weight is, and you
could use the address of (&) operator on them.

You can do this in C or C++:

my_data_type mdp;
mdp.height = &weight;

</Jack>



Wed, 06 Sep 2000 03:00:00 GMT  
 How to use alias within a structure?

  { Followups set to clc++,clc++m to remove clutter from the other
    groups.  This is an excellent C answer to a C++ question which
    was crossposted.  -mod }

: I want alias a variable within a structure, ie. an integer refered to as
: hieght or weight depending on the context. I'm having touble with the
: syntax.

: typedef struct
: {
:       int     weight;
:       int     &height = weight;   // alway gets syntax error here

You can't initialise a structure this way in C.  And even if you could,
what you are trying to initialise is a typedef, a definition of a type;
there's nothing there to initialise.  There was a post a couple of days
ago on how to get around this for a structure containing pointers to
functions; try checking back in the newsgroup.

:       ......
: }
: my_data_type;

: Strangely enough, I have no problem with aliasing a single variable such
: as:
:       int     iv;
:       int     &rv = iv;

That should give you a compiler error; the normal way to do this
would be

   int iv, *rv = &iv;

Assuming that's in the body of the program, not in another structure,
that's permitted.

Will


      [ about comp.lang.c++.moderated. First time posters: do this! ]



Thu, 07 Sep 2000 03:00:00 GMT  
 How to use alias within a structure?

 { Followups set to clc++,clc++m to reduce clutter in other groups. -jep }

Try this:

///////////////////////////////////////////////////////////////////////////////
typedef struct data_type
{
        int     weight;
        int     &height;

        data_type() : height(weight) {};

Quote:
}

my_data_type;

///////////////////////////////////////////////////////////////////////////////
void main()
{
    data_type dt;
    dt.height = 1;
    dt.weight = 2;
    cout << dt.height << endl;

Quote:
}

///////////////////////////////////////////////////////////////////////////////

References are init in the ctor init list.

However, if the example is a real one, then there is a problem with the
design IMHO.

Quote:

> I want alias a variable within a structure, ie. an integer refered to as
> hieght or weight depending on the context. I'm having touble with the
> syntax.

> typedef struct
> {
>         int     weight;
>         int     &height = weight;       // alway gets syntax error here
>         ......
> }
> my_data_type;

> Strangely enough, I have no problem with aliasing a single variable such
> as:
>         int     iv;
>         int     &rv = iv;

> Please help me out by kindly showing me some examples. Thanks.

> PS. I try not to use any overloading if possible.

--
Cristian Georgescu
_________________________________________________
    Smiths Industries
    Aerospace & Defense Systems
    7-9 Vreeland Road,
    Florham Park, NJ 07932, USA.
_________________________________________________


      [ about comp.lang.c++.moderated. First time posters: do this! ]



Thu, 07 Sep 2000 03:00:00 GMT  
 How to use alias within a structure?

 { Followups set to clc++,clc++m to reduce clutter in other groups. -jep }



Quote:
> I want alias a variable within a structure, ie. an integer refered to as
> hieght or weight depending on the context. I'm having touble with the
> syntax.

> typedef struct
> {
>    int     weight;
>    int     &height = weight;   // alway gets syntax error here
>    ......
> }
> my_data_type;

> Strangely enough, I have no problem with aliasing a single variable such
> as:
>    int     iv;
>    int     &rv = iv;

> Please help me out by kindly showing me some examples. Thanks.

The simple answer is: You can't.  A reference is (conceptually) more like a
pointer than a simple alias.  For example, you could do the following:

int &z = (x > y) ? x : y;

In case you are unfamiliar with the ?: operator, this code section makes z
into a reference to the bigger of x and y.  The way most C++ programmers
would approach your problem is through member functions:

struct my_data_type { // No need for typedefs in C++
  int weight_or_height;

  int get_weight(void) { return weight_or_height; }
  void set_weight(int i) { weight_or_height = i; }
  int get_height(void) { return weight_or_height; }
  void set_height(int i) { weight_or_height = i; }

Quote:
};

You can now do the following:

my_data_type my_variable;
my_variable.set_weight(5);
printf("Weight = %d Height = %d\n", my_variable.get_weight(),  
my_variable.get_height());

Another way of doing the same thing with shorter syntax is:

struct my_data_type {
  int weight_or_height;
  int &weight(void) { return weight_or_height; }
  int &height(void) { return weight_or_height; }

Quote:
};

my_data_type my_variable;
my_variable.weight() = 5;
printf("Weight = %d Height = %d\n", my_variable.weight(),
my_variable.height());

If you use this syntax, you can use weight and height like in your original
program, you just have to remember to add parenthesis.  Both of these
solutions should have all their functions inlined, so they are not any
slower or more wasteful than your original code.  In fact, they use less
space, since references take as much space as pointers, that is 4 bytes on
most computers.

--


      [ about comp.lang.c++.moderated. First time posters: do this! ]



Thu, 07 Sep 2000 03:00:00 GMT  
 How to use alias within a structure?

 { Followups set to clc++,clc++m to reduce clutter in other groups. -jep }

Quote:

> I want alias a variable within a structure, ie. an integer refered to as
> hieght or weight depending on the context. I'm having touble with the
> syntax.

        You probably want a "union" rather than a "struct"...look up "union" and see
if that's what you want...

Quote:
> typedef struct
> {
>         int     weight;
>         int     &height = weight;       // alway gets syntax error here
>         ......
> }
> my_data_type;

        OTOH, the reason why go get an error above is that in struct's, classes, and
probably unions, you cannot initialize a variable. That includes reference
variables. For example, the following also will not work:

struct
{
        int     i=0;

Quote:
};

        Member variables do not need initializers because that's the reason for
"constructors." In your constructor, you should be setting the default or
initial values of your member variables. Look up constructors and then make the
assignment statement in your constructor.

Quote:
> Strangely enough, I have no problem with aliasing a single variable such
> as:
>         int     iv;
>         int     &rv = iv;

        The above is no problem AS LONG AS IT's NOT inside a class, struct, or union -
where you don't have the ability to define a constructor.

--------

http://www.jhunix.hcf.jhu.edu/~hbien
3925 Beech Ave.
Apartment 214A
Baltimore, MD 21211-2255
Tel: 410-243-6091


      [ about comp.lang.c++.moderated. First time posters: do this! ]



Thu, 07 Sep 2000 03:00:00 GMT  
 How to use alias within a structure?

 { Followups set to clc++,clc++m to reduce clutter. -jep }


: I want alias a variable within a structure, ie. an integer refered to as
: hieght or weight depending on the context. I'm having touble with the
: syntax.

: typedef struct
: {
:       int     weight;
:       int     &height = weight;   // alway gets syntax error here
:       ......
: }
: my_data_type;

You can not initilize data members like this. Try this instead:

struct my_data_type {
        int     weight;
        int     &height;

        my_data_type() : wieght(0), height(weight) {}

Quote:
};

Oleg.

P.S. Wouldn't it be easier to just use an unnamed union?

typedef struct
{
   union {
      int     weight;
      int     &height = weight;       // alway gets syntax error here
   };
      ......

Quote:
}

my_data_type;

P.P.S. All this is a really bad style. Never have any public
data members. Try this instead:

class my_data_type {
   private:
        int     weight_height_;
   public:
        int& weight() { return weight_height_; }
        int& height() { return weight(); }

        int weight() const { return weight_height_; }
        int height() const { return weight(); }

Quote:
};

Oleg.
--
Life is a {*filter*}ly transmitted, 100% lethal disease.


      [ about comp.lang.c++.moderated. First time posters: do this! ]



Thu, 07 Sep 2000 03:00:00 GMT  
 How to use alias within a structure?

Quote:

>I want alias a variable within a structure, ie. an integer refered to
as
>hieght or weight depending on the context. I'm having touble with the
>syntax.

You can use anonymous unions in C++ to achieve this effect (see code
below).
Note that this approach is more space efficient, and safer, since
"height"
cannot refer to anything but the same value as "weight".

Andrew

// BEGIN SAMPLE
// ------------------------------
#include <stdio.h>

typedef struct _mystruct {
   union {
      int weight;
      int height;
   };

Quote:
} mystruct;

void main() {
   mystruct a;

   a.weight = 100;

   // This line should print "a.height is 100".
   printf("a.height is %i\n", a.height);

Quote:
}

// ------------------------------
// END SAMPLE

Quote:

>I want alias a variable within a structure, ie. an integer refered to
as
>hieght or weight depending on the context. I'm having touble with the
>syntax.

>typedef struct
>{
> int weight;
> int &height = weight; // alway gets syntax error here
> ......
>}
>my_data_type;

>Strangely enough, I have no problem with aliasing a single variable
such
>as:
> int iv;
> int  &rv = iv;

>Please help me out by kindly showing me some examples. Thanks.

>PS. I try not to use any overloading if possible.


>      [ about comp.lang.c++.moderated. First time posters: do this! ]


      [ about comp.lang.c++.moderated. First time posters: do this! ]


Thu, 07 Sep 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. structures within structures

2. Q about structures within structures

3. Initialising Structure Within Structure...How?!

4. Problem using structures in structures

5. Structue within a structure

6. how to dump easy-to-read structures from within C programs

7. help: unnamed unions within structures

8. pointers within an array of structures

9. Sorting a last names that are contained within a structure

10. Union within structures

11. Help in initializing function pointers within structures

12. Nesting a Structure within itself?

 

 
Powered by phpBB® Forum Software