creating alias for a class object

Discussion in 'C++' started by KK, Nov 17, 2005.

  1. KK

    KK Guest

    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::printMyself()
    {
    for (int i=0;i<10;i++)
    //print the entire array :: makes my life so easy with
    indexing
    }
    void somefunc(Country& 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
    KK, Nov 17, 2005
    #1
    1. Advertising

  2. KK

    Ron Natalie Guest

    KK wrote:
    > 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::printMyself()
    > {
    > for (int i=0;i<10;i++)
    > //print the entire array :: makes my life so easy with
    > indexing
    > }
    > void somefunc(Country& 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
    >


    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"];
    Ron Natalie, Nov 17, 2005
    #2
    1. Advertising

  3. KK

    Marcus Kwok Guest

    KK <> wrote:
    > 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 );
    > }

    [snip]
    > void somefunc(Country& 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
    > }


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

    --
    Marcus Kwok
    Marcus Kwok, Nov 18, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?aXdlYg==?=
    Replies:
    1
    Views:
    324
    Alexey Smirnov
    Apr 23, 2007
  2. grocery_stocker
    Replies:
    9
    Views:
    753
    grocery_stocker
    May 24, 2008
  3. Hoo87
    Replies:
    0
    Views:
    607
    Hoo87
    Jan 27, 2010
  4. Replies:
    14
    Views:
    184
    C.DeRykus
    Dec 22, 2009
  5. valtih1978
    Replies:
    1
    Views:
    610
Loading...

Share This Page