简体   繁体   English

有什么方法可以在不引用其模板类型的情况下向前声明指向类的指针?

[英]Is there any way to forward declare a pointer to a class without referencing its template type?

In foo.h: 在foo.h中:

#include <vector>
class A {
    int x;
};
typedef std::vector<A> AVector;

What I want is to put this in bar.h: 我想要的是将其放在bar.h中:

#include <vector>
<<< some forward declaration of AVector here>>>;

class B {
    AVector *myVector;
};

I would like to forward declare "AVector" into bar.h without bar.h having to include the definition of class A. Since class B only needs to know it is a pointer and not the details inside the pointer, I was hoping there was some way to declare this. 我想在bar.h中将声明“ AVector”转发到bar.h中,而不必包含类A的定义。由于类B只需要知道它是一个指针,而不是指针内部的详细信息,所以我希望声明这种方式。

Note: I am aware that I could just declare "void *" and then cast at various points. 注意:我知道我可以先声明“ void *”,然后在各个点进行强制转换。 I would rather not lose the type safety. 我宁愿不失去类型安全性。 Any solution that does lose the type safety isn't going to solve my problem. 任何失去类型安全性的解决方案都无法解决我的问题。

From Jean-BaptisteYunès comment on the question, you can forward declare a std::vector without the full definition of A. That means this works: 从Jean-BaptisteYunès对这个问题的评论中,您可以向前声明一个std :: vector而不使用A的完整定义。这意味着它起作用:

class A;
typedef std::vector<A> AVector;
class B {
    AVector *myVector;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM