简体   繁体   English

在C ++中使用C头文件中的Extern变量

[英]Using Extern variables from C header files with C++

I have a library that is written for me in C. In the library there is a variable that I need to use In foo.h (which is in C) 我有一个用C为我编写的库。在库中,我需要在foo.h(在C中)中使用一个变量。

extern mystruct foobar;

In bar.h (which is in C++) I have the following class. 在bar.h(在C ++中)中,我有以下课程。

#include foo.h

class myfoo { 

private:
   mystruct foobar;
}

What I would like to know is if I create an array of myfoo, will each instance of foobar be referencing the same variable, or will each instance be its own unique instantiation of the myfoo that independent from the other myfoo foobars? 我想知道的是,如果我创建一个myfoo数组,是foobar的每个实例引用相同的变量,还是每个实例都是独立于其他myfoo foobar的myfoo的唯一实例?

Here's the deal: you are not using foo.h 's foobar variable in bar.h (this is particularly true because foo.h only declares foobar , a definition would have to be in an implementation file). 这就是问题所在:您不在 bar.h使用foo.hfoobar变量(这尤其如此,因为foo.h声明 foobar ,定义必须在实现文件中)。 Rather, you are redeclaring a variable with the same name and type in the scope of the class. 相反,您要在类的范围内重新声明具有相同名称和类型的变量。

With that, all the normal rules for an instance member variable apply. 这样,适用于实例成员变量的所有常规规则。 In particular, every instance of myfoo will have its own instance of foobar . 特别是, myfoo每个实例都将具有自己的foobar实例。

If you removed the declaration from foo.h nothing would change: the declaration is completely irrelevant for your bar.h file. 如果从foo.h删除了声明, foo.h会有任何改变:该声明与bar.h文件完全无关。

The foobar member inside the class definition of myfoo is defined at a different scope from the foobar at global scope. myfoo类定义内的foobar成员在与全局范围内的foobar不同的范围内定义。 The are different variables, unrelated to each other, except that they are of the same type, and happen to have the same name (although at different scopes). 它们是互不相关的不同变量,只是它们的类型相同,并且名称相同(尽管作用域不同)。

If you create an array of myfoo objects, each one will have an instance of mystruct . 如果创建myfoo对象的数组,则每个对象都有一个mystruct实例。 All of those instances will be separate from the one declared in the global scope. 所有这些实例将与在全局范围内声明的实例分开。

What I would like to know is if I create an array of myfoo, will each instance of foobar be referencing the same variable, or will each instance be its own unique instantiation of the myfoo that independent from the other myfoo foobars? 我想知道的是,如果我创建一个myfoo数组,是foobar的每个实例引用相同的变量,还是每个实例都是独立于其他myfoo foobar的myfoo的唯一实例?

if you do this: 如果您这样做:

#include foo.h

class myfoo 
{ 
private:
    mystruct foobar;
};

void func()
{
    myfoo f[3];
    // ...
}

you create 3 different myfoo objects, each with its own foobar instance. 您将创建3个不同的myfoo对象,每个对象都有其自己的foob​​ar实例。

if you change the myfoo declaration as follows: 如果您如下更改myfoo声明:

#include foo.h

class myfoo 
{
private:
   static mystruct foobar;
};

// this extra declaration is required for 
// static member variables
mystruct myfoo::foobar;

void func()
{
    myfoo f[3];
    // ...
}

then the three instances of myfoo will share their single instance of foobar. 那么 myfoo的三个实例将共享它们的foobar单个实例。

NOTE : 注意

The class myfoo declaration may be in either a '.h' or a '.cpp' file. class myfoo声明可以在“ .h”或“ .cpp”文件中。

The mystruct myfoo::foobar; mystruct myfoo::foobar; declaration may only appear once, so it normally has to be in a '.cpp' file (or '.cc' or whatever you use). 声明只能出现一次,因此通常必须在“ .cpp”文件(或“ .cc”或您使用的任何文件)中。

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

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