Search code examples
c++classsyntaxsemantics

C++ new followed by class


A friend and I are working on an ancient C++ codebase. In this code, an array of CDelayEvent pointers is defined as follows

class CDelayEvent* m_pDelayEventList[DEF_MAXDELAYEVENTS];

and various objects in it are allocated in the following way

m_pDelayEventList[i] = new class CDelayEvent;

My question here is the use of the keyword class in both lines. The code compiles both lines with and without it.

  1. Is this valid C++? It compiles, but we have never seen this before.
  2. What does the class keyword do in this context?
  3. Is it safe/better to remove it everyhwere? Does it change anything at all?

I could not find anything about this problem because searching for "new class" only finds unrelated articles and questions.


Solution

  • Do you know C? Then you know how a structure is used? And that you need to use the struct keyword when e.g. declaring a variable of the structure?

    In C++ structures and classes are really the same, and C++ made the need for the struct and class keywords optional so a structure or a class could be used as a type.

    So in your case you can use class CDelayEvent or just CDelayEvent, they are semantically the same.