Q:

How to access members of the namespace in different files?

0

How to access members of the namespace in different files?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Amswer:

With help of an extern keyword, we can do this, see the below example code.

//common.h
#ifndef COMMON_H_INCLUDED
#define COMMON_H_INCLUDED
namespace ATIC
{
  extern int data;
}
#endif // COMMON_H_INCLUDED

 

//test.cpp
#include "common.h"
namespace ATIC
{
  int data = 27;
}

 

//main.cpp
#include <iostream>
#include "common.h"
int main()
{
    std::cout << ATIC::data << std::endl;
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now