473,394 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Can one class have two names?

class x,y
{
public:
.....
private:
};

Jul 22 '05 #1
9 2156
JustSomeGuy wrote:
class x,y
{
public:
....
private:
};

No. You can have one base class containing common
stuff and have the two inherit from it:
class base
{
// ...
};

class x
: public base
{
// ...
};

class y
: public base
{
// ...
};

Or you can have two instances of one class:
class common
{
} x, y; // two variable of class common.

Or you can throw the common stuff into
a template.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #2
JustSomeGuy wrote:
class x,y
{
public:
....
private:
};


No, you can't do that, but you could do this:

class x { /* ... */ };

typedef x y;

- Adam

--
Reverse domain name to reply.

Jul 22 '05 #3
JustSomeGuy wrote:
class x,y
{
public:
....
private:
};

Thomas's post covers most of it.

One thing he left out was typedefs.

class x
{
public:
.....
private:
};

typedef x y;

Now, x and y are exactly the same. However, y is really class x.

i.e.

void foo( y & );

and

void foo( x & );

are exactly the same declaration.
Jul 22 '05 #4
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote...
JustSomeGuy wrote:
class x,y
{
public:
....
private:
};

No. You can have one base class containing common
stuff and have the two inherit from it:
class base
{
// ...
};

class x
: public base
{
// ...
};

class y
: public base
{
// ...
};

Or you can have two instances of one class:
class common
{
} x, y; // two variable of class common.

Or you can throw the common stuff into
a template.


Or you could use 'typedef' thereby creating a synonym for
your class (hey, isn't a synonym a "second name"?) ....
Jul 22 '05 #5
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:br********@dispatch.concentric.net...
JustSomeGuy wrote:
class x,y
{
public:
....
private:
};

Thomas's post covers most of it.

One thing he left out was typedefs.

class x
{
public:
....
private:
};

typedef x y;

Now, x and y are exactly the same. However, y is really class x.


They are not exactly the same.
While you can declare x in a forward declaration as a class, you cannot do
the same for y:

class x; // ok
class y; // error
Jul 22 '05 #6
Eugene Alterman wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:br********@dispatch.concentric.net...
JustSomeGuy wrote:
class x,y
{
public:
....
private:
};

Thomas's post covers most of it.

One thing he left out was typedefs.

class x
{
public:
....
private:
};

typedef x y;

Now, x and y are exactly the same. However, y is really class x.

They are not exactly the same.
While you can declare x in a forward declaration as a class,
you cannot do the same for y:


Sure you can:

class x; // OK
typedef x y; // also OK

Jul 22 '05 #7
"Eugene Alterman" <Eu*************@autodesk.com> wrote in message
news:Qp******************@newssvr32.news.prodigy.c om...
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:br********@dispatch.concentric.net...
typedef x y;

Now, x and y are exactly the same. However, y is really class x.


They are not exactly the same.
While you can declare x in a forward declaration as a class, you cannot do
the same for y:

class x; // ok
class y; // error


Yes, and this can be a pain sometimes. It would be nice if the language did not take 'class y'
literally (a little like 'class' as a template argument), and instead as just 'some type' for
the purpose of declaring pointers or references, or whatever the forward declaration is for. If
'y' turns out to be some incompatible type, such as a reference, the compiler can catch it
later, just as does now if 'y' turns out to be a struct rather than a class.

DW

Jul 22 '05 #8

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:br***********@nserve1.acs.ucalgary.ca...
class x,y
{
public:
....
private:
};


Not like that. But you can just use typedef (without knowing exactly what
you're trying to do.)
Jul 22 '05 #9

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:br***********@nserve1.acs.ucalgary.ca...
class x,y
{
public:
....
private:
};


Yes:

typedef class x
{
public:
.....
private:
} y;

Cheers!

- Risto -

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mohammed Mazid | last post by:
Can anyone please tell me if there is a class that provides place names in the world? I am trying to implement a Flight system and if there was such a class then it could've contributed towards...
3
by: gry | last post by:
I often find myself wanting an instance attribute that can take on only a few fixed symbolic values. (This is less functionality than an enum, since there are no *numbers* associated with the...
8
by: Ares Lagae | last post by:
When adopting the coding style of the standard C++ library, you often run into naming problems because class names are lower case, and member functions do not have get/set prefixes. For example:...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
5
by: meyousikmann | last post by:
Given these two (incomplete but representative) classes in two seperate header files: Class1.h class Class1 { public: Class(const char CharValue, const int IntValue1, const int IntValue2);...
6
by: Alex Hunsley | last post by:
I know that I can catch access to unknown attributes with code something like the following: class example: def __getattr__(self, name): if name == 'age': return __age else: raise...
9
by: David A. Osborn | last post by:
I have a set of classes that each have an enumeration in them, and based on dynamic input I need to access a different enumeration. For example Three classes Class_A, Class_B, and Class_C that...
6
by: alainpoint | last post by:
Hello, I have got a problem that i can't readily solve. I want the following: I want to create a supertuple that behaves both as a tuple and as a class. It should do the following:...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
4
by: Steven Simpson | last post by:
Stefan Ram wrote (in "More than one language in a page"): Is this a new trend of user-agent writers (Microformats, and now Google) staking claims on the @class namespace? I'm surely not the only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.