gpt4 book ai didi

c++ - 指针和地址

转载 作者:行者123 更新时间:2023-11-28 06:15:04 24 4
gpt4 key购买 nike

我只是稍微思考一下,然后我看到了一些对我来说没有意义的东西。该应用程序几乎要求您输入一个类的名称,如果它在 map 中找到它,它就会创建该类的一个实例并打印出该对象的地址。

我第一次输入 Derived1,它生成 1 个地址,例如 (0x00001),第二次我输入相同的名称,它显示另一个地址 (0x00002),第三次它再次使用相同的地址。

为什么我只得到第二个对象的新地址而没有得到其余对象的新地址?

这绝不是问题,我只是好奇。

#include <iostream>
#include <memory>
#include <map>
#include <string>
#include <vector>
#include <typeinfo>

class Base
{
protected:
int id;
public:
Base(int _id)
:id(_id)
{
std::cout << "Im the base class, and this is my address: " << this << std::endl;
}
virtual void PrintMyAddress() const = 0;
void PrintId()
{
std::cout << "This is my ID: " << this->id << std::endl;
}
virtual ~Base(){}
};

class Derived1 : public Base
{
public:
Derived1(int _id) : Base(_id){}
void PrintMyAddress() const override
{
std::cout << "I'm derived 1, this is my address: " << this << std::endl;
}
~Derived1(){}
};

class Derived2 : public Base
{
public:
Derived2(int _id) : Base(_id){}
virtual void PrintMyAddress() const override
{
std::cout << "I'm derived 2, this is my address: " << this << std::endl;
}

virtual ~Derived2(){}
};

class Derived3 : public Derived2
{
public:
Derived3(int _id) : Derived2(_id){}
void PrintMyAddress() const override
{
std::cout << "I'm derived 3, this is my address: " << this << std::endl;
}

~Derived3(){}
};

class Generate
{
private:
typedef std::unique_ptr<Base> (Generate::*SomeFunction)(int);
std::map<std::string, SomeFunction> listFunction;

public:
Generate()
{
this->MapClasses();
}

template <typename T>
std::unique_ptr<Base> CreateDerived(int _id)
{
std::unique_ptr<Base> tmp(new T(_id));
return std::move(tmp);
}

void MapClasses()
{
this->listFunction["Derived1"] = &Generate::CreateDerived<Derived1>;
this->listFunction["Derived2"] = &Generate::CreateDerived<Derived2>;
this->listFunction["Derived3"] = &Generate::CreateDerived<Derived3>;
}

std::unique_ptr<Base> Class(std::string _name)
{
if (this->listFunction.find(_name) != this->listFunction.end())
{
return std::move((this->*this->listFunction[_name])(rand() % 100));
}
else
{
std::unique_ptr<Base> itsnull;
return std::move(itsnull);
}
}

std::map<std::string, SomeFunction>& GetFunctionList()
{
return this->listFunction;
}
};


int main()
{
Generate gen;
bool run = true;
while (run)
{
std::cout << std::endl << "What class do you want to generate? Type in the name of the class: " << std::endl << std::endl;
std::string className;
std::cin >> className;
if (className == "exit")
{
run = false;
continue;
}

auto genclass = gen.Class(className);
if (genclass.get())
{
genclass->PrintMyAddress();
genclass->PrintId();
std::cout << "This is my TypeID: " << typeid(*genclass).name() << std::endl;
}
else
{
std::cout << "Class couldn't be created because it doesn't exist..." << std::endl;
}
}

std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return 0;
}

这是一个类似的示例,但始终显示相同的地址:

for (int i = 0; i < 10; ++i)
{
int* test = new int;
std::cout << test << std::endl;
delete test;
}

编译器是否在第二次创建后优化了一切?

最佳答案

您的代码基本上创建了一个新对象,然后在每个循环中将其删除。

对象的地址取决于堆把它放在哪里。堆可以自由地重用最近释放的空间(如果不这样做,那将是愚蠢的),因此您很可能会得到重复的地址。

关于c++ - 指针和地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30479728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com