S
S
Satehill2021-06-25 13:40:47
C++ / C#
Satehill, 2021-06-25 13:40:47

Why write class in class object declaration?

Usually they don’t write like that, but I saw one code where it is almost everywhere.

For example:

class prettyClass
{
    // just class
}

int main()
{

 class prettyClass obj; // class?

std::cin.get();
return 0;

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2021-06-25
@Satehill

If a function or a variable exists in scope with the name identical to the name of a class type, class can be prepended to the name for disambiguation, resulting in an elaborated type specifier.

class Foo;  // forward declaration of a class
 
class Bar { // definition of a class
  public:
    Bar(int i) : m_i(i) {}
  private:
    int m_i;
};
 
template <class T> // template argument
void qux() {
    T t;
}
 
int main()
{
    Bar Bar(1);
    class Bar Bar2(2); // elaborated type
}

https://en.cppreference.com/w/cpp/keyword/class
If used everywhere, then, IMHO, this is a matter of taste. The person just loves it.

N
nedosekinstanislav, 2021-06-25
@nedosekinstanislav

Usually they don’t write .. are you serious?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question