访问未声明的结构?

我有办法访问尚未声明的结构吗?

//Need to some how declare 'monitor' up here, with out moving 'monitor' above 'device'
//because both structs need to be able to access each others members

struct{
    int ID = 10;
    int Get__Monitor__ID(){
        return monitor.ID; //obvioulsly 'monitor' is not declared yet, therefore throws error and is not accessible
    }
} device;

struct{
    int ID = 6;
    int Get__Device__ID(){
        return device.ID; //because 'device' is declared above this struct, the ID member is accessible
    }
} monitor;

回答

在这种特殊情况下,您可以在结构体中定义函数原型,定义可以稍后来。

struct device_t {
    int ID = 10;
    int Get__Monitor__ID();
} device;

struct monitor_t {
    int ID = 6;
    int Get__Device__ID();
} monitor;

int device_t::Get__Monitor__ID() {
  return monitor.ID;
}

int monitor_t::Get__Device__ID() {
  return device.ID;
}

  • You probably want to add `inline` unless the functions are moved to CPP file. And only first fonction need to be modified.

以上是访问未声明的结构?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>