By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Can one class have two names?

P: n/a
class x,y
{
public:
.....
private:
};

Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.