creating alias for a class object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KK

    #1

    creating alias for a class object

    Hi,
    How can I create alias for a class object? Please go thru this
    program to understand the motivation.

    Class Name;
    Class Country{
    Name list[10];
    /*My intension is to have:
    list[0] - usa
    list[1] - uk
    list[2] - ..
    */
    //define other essential member functions
    void PrintMyself (void );
    }
    void Country::PrintM yself()
    {
    for (int i=0;i<10;i++)
    //print the entire array :: makes my life so easy with
    indexing
    }
    void somefunc(Countr y& ex)
    {
    Name usa_country;
    usa_country =ex.list[0]; //indexing is no good here - I wish I had
    the alias for list[0] as usa
    }
    Hoping to get the answer.
    Thank you.
    KK

  • Ron Natalie

    #2
    Re: creating alias for a class object

    KK wrote:[color=blue]
    > Hi,
    > How can I create alias for a class object? Please go thru this
    > program to understand the motivation.
    >
    > Class Name;
    > Class Country{
    > Name list[10];
    > /*My intension is to have:
    > list[0] - usa
    > list[1] - uk
    > list[2] - ..
    > */
    > //define other essential member functions
    > void PrintMyself (void );
    > }
    > void Country::PrintM yself()
    > {
    > for (int i=0;i<10;i++)
    > //print the entire array :: makes my life so easy with
    > indexing
    > }
    > void somefunc(Countr y& ex)
    > {
    > Name usa_country;
    > usa_country =ex.list[0]; //indexing is no good here - I wish I had
    > the alias for list[0] as usa
    > }
    > Hoping to get the answer.
    > Thank you.
    > KK
    >[/color]

    Well you've not really succinctly defined the problem. You could just
    do
    #define usa ex.list[0]

    but I suspect that's not what you're after.

    If you sant to map the string "usa" into one of your Name classes,
    how about a std::map?

    map<string, Name> list;
    list["usa"] = //...
    list["uk"] = /...

    Name usa_country = list["usa"];

    Comment

    • Marcus Kwok

      #3
      Re: creating alias for a class object

      KK <kewlkarun@yaho o.com> wrote:[color=blue]
      > Hi,
      > How can I create alias for a class object? Please go thru this
      > program to understand the motivation.
      >
      > Class Name;
      > Class Country{
      > Name list[10];
      > /*My intension is to have:
      > list[0] - usa
      > list[1] - uk
      > list[2] - ..
      > */
      > //define other essential member functions
      > void PrintMyself (void );
      > }[/color]
      [snip][color=blue]
      > void somefunc(Countr y& ex)
      > {
      > Name usa_country;
      > usa_country =ex.list[0]; //indexing is no good here - I wish I had
      > the alias for list[0] as usa
      > }[/color]

      The way you have it, list is private, and somefunc() has not been
      declared as a friend.

      --
      Marcus Kwok

      Comment

      Working...