Combination of C ++ youyuan class and namespace

Source: Internet
Author: User

I encountered a small problem when I used the object meta class in combination with the namespace today. Let's summarize it.

This scenario:

A is a connection class and B is a client class. Different namespaces are defined in different header files,

A wants B to be able to access its own private members, so B is set as its friend Meta class,

Class B needs class A objects for initialization during instantiation (constructors use Class A object pointers ).

 

This involves the mutual inclusion of header files. In the header file, the required class types are declared forward (Forward Declaration), and then the. cpp file contains header files of this class type,

For more information, see http://www.cnblogs.com/sunrack/articles/590384.html

 

When you declare the types of different namespaces in the forward direction, the following columns are declared. When using this type, you must specify the namespace in which it is located.

namespace space_name {class type_name;}

 

There is a problem here,

If a class (type name is a) is in a specific namespace (name is NSA), and its membership class (type name is B) is in a global space, when you declare an object meta class, the target class uses the global space domain operator,

Otherwise, Class A will think that Class B is under NSA, and Class B is actually in the global space, so during compilation, prompt that Class B has no permission to use private data or function members of Class.

In Linux, this rule needs to be strictly followed. In Windows, Dev-C ++ finds that even if Class B does not specify the global space, it can be compiled and run normally, maybe the compiler implementation is different ..

To sum up, you must specify the namespace where the user Meta class declaration involves different namespaces.

 

The Demo code is as follows:

A.h

#ifndef A_H#define A_H//forward declarationnamespace nsb {class B;}namespace nsa {class A {public:    A(): num_(0) {}    int get_num() const { return num_; }    friend class nsb::B;    //friend class ::B; //if B is in global namespace, need to use "::B"private:    int num_;};} // namespace#endif

B. H

#ifndef B_H#define B_H//forward declarationnamespace nsa {class A;}namespace nsb {class B {public:    B(nsa::A *a);    void set_num_of_A(int n);    int get_num_of_A() const;private:    nsa::A *pa_;};} // namespace#endif

B. cpp

#include "b.h"#include "a.h"namespace nsb {B::B(nsa::A *pa): pa_(pa) {}void B::set_num_of_A(int n) {    pa_->num_ = n;}int B::get_num_of_A() const {    return pa_->num_;}} // namespace

Test. cpp

#include "a.h"#include "b.h"#include <iostream>using std::cout;using std::endl;int main() {    nsa::A a;    nsb::B b(&a);    cout << a.get_num() << endl;    b.set_num_of_A(100);    cout << b.get_num_of_A() << endl;    return 0;}

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.