Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   creating alias for a class object (http://www.velocityreviews.com/forums/t449880-creating-alias-for-a-class-object.html)

KK 11-17-2005 10:33 PM

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


Ron Natalie 11-17-2005 11:02 PM

Re: creating alias for a class object
 
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"];

Marcus Kwok 11-18-2005 02:24 PM

Re: creating alias for a class object
 
KK <kewlkarun@yahoo.com> 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


All times are GMT. The time now is 09:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57