Can one class have two names?

T

Thomas Matthews

JustSomeGuy said:
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
 
G

Gianni Mariani

JustSomeGuy said:
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.
 
V

Victor Bazarov

Thomas Matthews said:
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"?) ....
 
E

Eugene Alterman

Gianni Mariani said:
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
 
E

E. Robert Tisdale

Eugene said:
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
 
D

David White

Eugene Alterman said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top