c ++向前声明一个静态类成员 [英] c++ forward declaration of a static class member

查看:150
本文介绍了c ++向前声明一个静态类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类:

class M {
public:
    static std::string t[];
};

我想在不同的类(头文件)中使用M :: t:

with an initialization that comes later. I want to use the M::t later in a different class (header file):

class Use {
public:
    void f() { std::cout << M::t[0] << std::endl; }
};

有没有办法实现这一点,而不包括使用的头文件的整个类M?我理解forward声明不允许访问类成员,但这个美丽是一个静态的,所以它不应该是一个巨大的问题,编译器..

Is there any way to achieve this without including the whole class M for the header file of the Use? I understand that forward declarations do not allow accessing class members, however this beauty is a static one, so it shouldn't be a huge problem for the compiler..

推荐答案

不,你不能。您可以在标题中包含标头,或者在实现文件中分隔实现 Use :: f ,并包含 M 的标题。

No, you can't. You can either include the header in the header, or separate the implementation Use::f in an implementation file and include M's header there.

这篇关于c ++向前声明一个静态类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆