我在使用虚拟继承的 C++ 程序中遇到了不同的 this 指针地址问题。由于存在差异,该问题导致我的 getter 返回默认值...
我在使用虚拟继承的 C++ 程序中遇到了不同指针地址的问题 this
。由于 this
指针地址的差异,该问题导致我的 getter 返回默认值。我在下面提供了一个简化的示例来说明该问题。请注意,这是我实际问题中所有类和层次结构的迷你示例。在这个例子中,一切正常,但是我在实际项目中遇到的错误是我有一个 ClientNodeTemplate
使用从基类虚拟继承的 ITemplate
。在程序中,我注意到 this
内部的指针地址 SetParamsToCopy
与其他方法(例如 getter)中观察到的地址不同。这种差异导致我的 getter 在调用后返回默认值而不是预期值,而调用 SetParamasToCopy()
本应将所有值从 更改为 false
。 true
这是我的代码:
#include <iostream>
#include <memory>
struct ITemplate
{
ITemplate() { std::cout << "ITemplate constructed -> " << this << std::endl; }
virtual ~ITemplate() { std::cout << "ITemplate destroyed -> " << this << std::endl; }
};
struct IClientNodeTemplate : virtual public ITemplate
{
virtual void SetParamsToCopy( bool copyLinks, bool copyDns, bool copyAddons, bool copyYaml, bool addMyNode ) = 0;
};
struct NodeTemplate : virtual public ITemplate
{
NodeTemplate() { std::cout << "NodeTemplate constructed -> " << this << std::endl; }
virtual ~NodeTemplate() { std::cout << "NodeTemplate destroyed -> " << this << std::endl; }
};
class ClientNodeTemplate : public NodeTemplate, virtual public IClientNodeTemplate
{
public:
ClientNodeTemplate() { std::cout << "ClientNodeTemplate constructed -> " << this << std::endl; }
~ClientNodeTemplate() { std::cout << "ClientNodeTemplate destroyed -> " << this << std::endl; }
void SetParamsToCopy( bool copyLinks, bool copyDns, bool copyAddons, bool copyYaml, bool addMyNode ) override
{
std::cout << "SetParamsToCopy -> " << this << std::endl;
copyLinks_ = copyLinks;
copyDns_ = copyDns;
copyAddons_ = copyAddons;
copyYaml_ = copyYaml;
addMyNode_ = addMyNode;
}
bool GetCopyLinks() const
{
std::cout << "GetCopyLinks -> " << this << std::endl;
return copyLinks_;
}
bool GetCopyDns() const
{
std::cout << "GetCopyDns -> " << this << std::endl;
return copyDns_;
}
bool GetCopyAddons() const
{
std::cout << "GetCopyAddons -> " << this << std::endl;
return copyAddons_;
}
bool GetAddMyNode() const
{
std::cout << "GetAddMyNode -> " << this << std::endl;
return addMyNode_;
}
bool GetCopyYaml() const
{
std::cout << "GetCopyYaml -> " << this << std::endl;
return copyYaml_;
}
void print()
{
std::cout << "Print address -> " << this << std::endl;
std::cout << GetCopyLinks() << std::endl
<< GetCopyDns() << std::endl
<< GetCopyAddons() << std::endl
<< GetAddMyNode() << std::endl
<< GetCopyYaml() << std::endl;
}
private:
bool copyLinks_ = false;
bool copyDns_ = false;
bool copyAddons_ = false;
bool addMyNode_ = false;
bool copyYaml_ = false;
};
int main()
{
std::unique_ptr<ClientNodeTemplate> client_node_template_ = std::make_unique<ClientNodeTemplate>();
std::cout << "Main -> " << client_node_template_.get() << std::endl;
client_node_template_->SetParamsToCopy(true, true, true, true, true);
client_node_template_->print();
std::cout << "Main after print -> " << client_node_template_.get() << std::endl;
return 0;
}
我期望它能像在任何普通的“getter-setter”情况下一样工作,比如当我调用 SetParamsToCopy 时,它会按预期工作,为数据成员分配适当的值。我想提醒一下,在这个“小版本”中一切都正常。但在更大的版本中会出现这样的错误:
输出
ITemplate constructed -> 0x58b1339d02b0
NodeTemplate constructed -> 0x58b1339d02b0
ClientNodeTemplate constructed -> 0x58b1339d02b0
Main -> 0x58b1339d02b0
SetParamsToCopy -> 0x58b1339d02b0 // here in my case 0x58b1339d02b3
Print address -> 0x58b1339d02b0
GetCopyLinks -> 0x58b1339d02b0
1
GetCopyDns -> 0x58b1339d02b0
1
GetCopyAddons -> 0x58b1339d02b0
1
GetAddMyNode -> 0x58b1339d02b0
1
GetCopyYaml -> 0x58b1339d02b0
1
Main after print -> 0x58b1339d02b0
ClientNodeTemplate destroyed -> 0x58b1339d02b0
NodeTemplate destroyed -> 0x58b1339d02b0
ITemplate destroyed -> 0x58b1339d02b0
我不需要修改代码,我需要关于什么会导致这样的问题的建议并帮助处理它,谢谢)