本文介绍了如何转发声明一个在命名空间中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在头文件中使用转发声明来减少#includes的使用,从而减少用户包含我头文件的依赖。

I am trying to use forward declarations in header files to reduce #includes used and hence reduce dependencies where users include my header file.

但是,我无法使用命名空间的向前decalre。请参见下面的示例。

However, I am unable to forward decalre where namespaces are used. See example below.

a.hpp file:
#ifndef __A_HPP__
#define __A_HPP__

namespace ns1 {

   class a {
   public:
      a(const char* const msg);

      void talk() const;

   private:
      const char* const msg_;
   };
}

#endif //__A_HPP__

a.cpp file:
#include <iostream>

#include "a.hpp"

using namespace ns1;

a::a(const char* const msg) : msg_(msg) {}

void a::talk() const {
   std::cout << msg_ << std::endl;
}



consumer.hpp file:
#ifndef __CONSUMER_HPP__
#define __CONSUMER_HPP__

// How can I forward declare a class which uses a namespace
//doing this below results in error C2653: 'ns1' : is not a class or namespace name
// Works with no namespace or if I use using namespace ns1 in header file
// but I am trying to reduce any dependencies in this header file
class ns1::a;

class consumer
{
public:
   consumer(const char* const text) : a_(text) {}
   void chat() const;

private:
   a& a_;
};

#endif // __CONSUMER_HPP__

consumer.cpp implementation file:
#include "consumer.hpp"
#include "a.hpp"

consumer::consumer(const char* const text) : a_(text) {}

void consumer::chat() const {
   a_.talk();
}

test - main.cpp file:
#include "consumer.hpp"

int main() {
   consumer c("My message");
   c.chat();
   return 0;
}



UPDATE:



UPDATE:

Here is my very contrived working code using the answer below.

a.hpp:
#ifndef A_HPP__
#define A_HPP__

#include <string>

namespace ns1 {

   class a {
   public:
      void set_message(const std::string& msg);
      void talk() const;

   private:
      std::string msg_;
   };

} //namespace

#endif //A_HPP__

a.cpp:
#include <iostream>
#include "a.hpp"

void ns1::a::set_message(const std::string& msg) {
    msg_ = msg;
}
void ns1::a::talk() const {
   std::cout << msg_ << std::endl;
}

consumer.hpp:
#ifndef CONSUMER_HPP__
#define CONSUMER_HPP__

namespace ns1
{
   class a;
}

class consumer
{
public:
   consumer(const char* text);
   ~consumer();
   void chat() const;

private:
   ns1::a* a_;
};

#endif // CONSUMER_HPP__

consumer.cpp:
#include "a.hpp"
#include "consumer.hpp"

consumer::consumer(const char* text) {
   a_ = new ns1::a;
   a_->set_message(text);
}
consumer::~consumer() {
   delete a_;
}
void consumer::chat() const {
   a_->talk();
}


main.cpp:
#include "consumer.hpp"

int main() {
   consumer c("My message");
   c.chat();
   return 0;
}


推荐答案

code> ::

To forward declare class type a in a namespace ns1:

namespace ns1
{
    class a;
}

转发在多级命名空间中声明一个类型:

To forward declare a type in multiple level of namespaces:

namespace ns1
{
  namespace ns2
  {
    //....
     namespace nsN
     {
        class a;
     }
    //....
  }
}


$ b b

您使用 a consumer 的成员,这意味着它需要具体类型, t这种情况下工作。

Your are using a a member of consumer which means it needs concrete type, your forward declaration won't work for this case.

这篇关于如何转发声明一个在命名空间中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 19:11