Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include typedef inside a class header

Tags:

c++

This is my header:

#ifndef TIMING_H
#define TIMING_H

#define MAX_MESSAGES 1000
typedef Message* MessageP; //inside the class?

class Timing {

public:

 Timing();

private:

 struct Message {
  Agent *_agent;
  double _val;
 };

 MessageP* _msgArr;
 int _waitingMsgs;


};

My question is: do I have to place the typedef inside the class block right above MessageP* _msgArr or is it OK to place it near all the #define?

It doesn't output compilation errors so I'm not sure about it.

like image 597
Asher Saban Avatar asked Sep 06 '10 18:09

Asher Saban


1 Answers

Outside of the class, that type must be referred as Timing::Message, so

typedef Timing::Message* MessageP;

But this is possible only after the declaration of Timing::Message, yet MessageP is used before the declaration of Timing is complete, so it's not possible.

Moreover, the struct is a private: member, so you can't define this outside anyway. The typedef must be done inside the Timing class.

class Timing {

public:

 Timing();

private:

 struct Message {
  Agent *_agent;
  double _val;
 };

 typedef Message* MessageP;   // <--


 MessageP* _msgArr;
 int _waitingMsgs;


};

You did not get compilation error probably because another Message type in global scope already exists.

like image 117
kennytm Avatar answered Sep 27 '22 21:09

kennytm