Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向类添加新成员变量是否会影响二进制兼容性?_C++_C++11 - Fatal编程技术网

C++ 向类添加新成员变量是否会影响二进制兼容性?

C++ 向类添加新成员变量是否会影响二进制兼容性?,c++,c++11,C++,C++11,我在共享库中有一个名为AMFactory的类。许多用户应用程序都使用此共享库。现在我想向类中添加一个新的私有成员变量。这会破坏二进制兼容性吗?如果它坏了,那么就有黑客攻击它。在我的例子中,我不希望重新编译用户应用程序。下面是我的类[AMFactory]及其基类[Factory]的片段 namespace CF { class CF_EXPORT Factory { public: virtual ~Factory(); virtual bool registerManager(const

我在共享库中有一个名为AMFactory的类。许多用户应用程序都使用此共享库。现在我想向类中添加一个新的私有成员变量。这会破坏二进制兼容性吗?如果它坏了,那么就有黑客攻击它。在我的例子中,我不希望重新编译用户应用程序。下面是我的类[AMFactory]及其基类[Factory]的片段

namespace CF {
class CF_EXPORT Factory {
 public:
 virtual ~Factory();

 virtual bool registerManager(const std::string &_interface,
                      const Id_t &_id) = 0;
 virtual bool unregisterManager(const std::string &_interface,
                      const Id_t &_id) = 0;
};
}
namespace CF {
namespace AM {
class AMFactory : public CF::Factory {
public:
CF_EXPORT static std::shared_ptr<AMFactory> get();

CF_EXPORT AMFactory();
CF_EXPORT virtual ~AMFactory();

CF_EXPORT bool registerManager(const std::string &_interface,
                      const Id_t &_id);
CF_EXPORT bool unregisterManager(const std::string &_interface,
                      const Id_t &_id);

private:
CF_EXPORT bool registerManager(std::shared_ptr<AMManager>);
CF_EXPORT bool unregisterManager(std::shared_ptr<AMManager>);

static std::shared_ptr<AMFactory> theFactory;
ServicesMap services_;
};
}
}
名称空间CF{
出口工厂{
公众:
虚拟工厂();
虚拟布尔寄存器管理器(const std::string&\u接口,
常数Id&(u Id)=0;
虚拟bool注销管理器(const std::string&\u接口,
常数Id&(u Id)=0;
};
}
命名空间CF{
名称空间AM{
AMFactory类:公共CF::Factory{
公众:
CF_EXPORT static std::shared_ptr get();
CF_EXPORT AMFactory();
CF_导出虚拟~AMFactory();
CF_导出布尔寄存器管理器(const std::string&_接口,
const Id&u Id);
CF_EXPORT bool unregisterManager(const std::string&_接口,
const Id&u Id);
私人:
CF_EXPORT bool registerManager(std::shared_ptr);
CF_导出bool unregisterManager(std::shared_ptr);
静态std::共享工厂;
服务地图服务;
};
}
}
现在我想向类中添加一个新的私有成员变量。这会破坏二进制兼容性吗

类的大小将发生变化,因此是的,它将破坏二进制兼容性

如果它坏了,那么就有黑客攻击它。在我的例子中,我不希望重新编译用户应用程序


一个我不知道的。这也是为什么我们在开始实施之前关注设计。

当然它破坏了二进制兼容性。它是否是私有的并不重要。一个成员就是一个成员,添加一个成员会改变对象的大小,更不用说过时的用户现在将使“未知成员”处于未初始化状态,等等。@下划线\u d:如果我将类更改为singleton,它是否仍然使“未知成员”处于未初始化状态?我不知道将其设置为singleton会有什么影响,但是,这也与所问的主要问题无关:您仍然会破坏ABI,并将未定义的行为引入所有调用代码。@下划线\u d:如果对象创建本身是通过同一类的静态函数进行的,那么为什么它会让“未知成员”保持未初始化状态?可能是我没有弄清楚这里的情况。好吧,我想使用create方法可以解决未初始化的成员,但这是你在这样破坏ABI时最不担心的。