2

I'm new to C++ and while I'm reviewing someone else's code, I ran into an expression I don't understand.

In the header file I have a normal class definition:

//in data.hpp
class DATA_C : public QThread
{
   Q_OBJECT
   // the rest of class definition...
}

A snippet of the source file:

//in data.cpp
class DATA_C data_container;

I can understand "DATA_C data", which is a declaration; but what is "class DATA_C data_container"? What does it do?

Thanks in advance.

megamonium
  • 463
  • 3
  • 7
  • I'm not 100% on this, but I suspect it's probably something to do with C, and is maybe answered here: https://stackoverflow.com/questions/7729646/using-struct-keyword-in-variable-declaration-in-c – Tas Sep 06 '18 at 03:30
  • you could point out where you found that code – eyllanesc Sep 06 '18 at 03:34
  • 2
    AFAIK,When instantiating a class object, you can use the Class keyword or not use it. – GeekLee Sep 06 '18 at 03:35
  • @eyllanesc this piece of code was written by one of my colleagues – megamonium Sep 06 '18 at 03:42

1 Answers1

1

In this context tt has exactly the same meaning as DATA_C data_container;. In many contexts you can optionally use the term class X , if it would have been valid to just use X.

In general you can use class X when X has not been defined yet and it declares the class (but doesn't define it). But class DATA_C data_container; would not be allowed if DATA_C had not been previously defined, because you cannot instantiate an object of incomplete type.

M.M
  • 138,810
  • 21
  • 208
  • 365