Can a name of a object be name of any other class?

  • Thread starter vishnupriya.sureshbabu
  • Start date
V

vishnupriya.sureshbabu

consider i have two classes A and B. Is it wrong declaring a object of
type A with name B?
class A{}
class B{}
A B;
Why is it wrong?
 
J

Jim Langston

consider i have two classes A and B. Is it wrong declaring a object of
type A with name B?
class A{}
class B{}
A B;
Why is it wrong?

Suprisingly, this compiles, and in my limited test actually works. I would
say it's wrong, but I can't say why. This actually compiles and produces
the output "10" which I would not expect.

Not suprisingly, you can't say
A A;
however. But if you could say A B; why couldn't you say A A;?

#include <iostream>
#include <string>

class A {
public:
int X;
friend std::eek:stream& operator<< (std::eek:stream& os, const A& Instance );
};

std::eek:stream& operator<< (std::eek:stream& os, const A& Instance )
{
os << Instance.X;
return os;
}

class B { };

int main()
{
A B;
B.X = 10;

std::cout << B << std::endl;

std::string wait;
std::cin >> wait;

return 0;
}
 
V

vishnupriya.sureshbabu

It will not give an error but it will not allow to instantiate an
object of type B. It does not identify the declaration of calss B. Can
anyone explain?
 
K

Kai-Uwe Bux

consider i have two classes A and B. Is it wrong declaring a object of
type A with name B?
class A{}
class B{}
A B;
Why is it wrong?

It is not.

[3.3/4]

Given a set of declarations in a single declarative region, each of which
specifies the same unqualified name,
? they shall all refer to the same entity, or all refer to functions and
function templates; or
? exactly one declaration shall declare a class name or enumeration name
that is not a typedef name and the other declarations shall all refer to
the same object or enumerator, or all refer to functions and function
templates; in this case the class name or enumeration name is hidden.


So what you are doing is not illegal, but you are hiding class B. As a
matter of coding style, however, I would stay away from such things.


Best

Kai-Uwe Bux
 
J

Jim Langston

It will not give an error but it will not allow to instantiate an
object of type B. It does not identify the declaration of calss B. Can
anyone explain?

Oh yeah! Now it makes sense. You are simply hiding the class B by the
definition of a variable B.

Same as this.

#include <iostream>
int i = 10;

int main()
{
int i = 20;
std::cout << i << std::endl; // prints 20, the global i is hidden
}

it's just in your case B is a class definition and an instance of another
class instead of both being ints. Makes sense now.
 
V

vishnupriya.sureshbabu

Though the instance of the class is overriding the class definition. So
if the class is redefined after the object is instantiated it should
work right? But it doesnt seem to.
Class A{}
Class B{}
A B;
Class B{}
B C;
It does not recognise class B which is used for instantiating C
 
B

Boris Kolpackov

Though the instance of the class is overriding the class definition. So
if the class is redefined after the object is instantiated it should
work right? But it doesnt seem to.
Class A{}
Class B{}
A B;
Class B{}
B C;
It does not recognise class B which is used for instantiating C

Since B-variable hides B-type, all unqualified references to B now
resolve to B-variable. To get to the type you will need to qualify
it:

class B C;

hth,
-boris
 
J

Jim Langston

Boris Kolpackov said:
I fail to see how it is related. Even if we disregard the fact that it
talks about C#, there is nothing remotely related to re-using names
of types and instances.

I think he replied to the wrong message. There is another message thread a
few down that he also replied with this same message wehre it actually was
related.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top