-
-
Notifications
You must be signed in to change notification settings - Fork 0
Exotic casts
Here I will mention the non-conventional casts used in Karma.
This is a cast for smart pointers. For example see here
void VulkanVertexArray::SetIndexBuffer(const std::shared_ptr<IndexBuffer>& indexBuffer)
{
indexBuffer->Bind();
m_IndexBuffer = std::static_pointer_cast<VulkanIndexBuffer>(indexBuffer);
GenerateVulkanVA();
}Note the Unreal Engine style of casting which doesn't use * for casting.
This cast is used to convert a pointer of certain data-type into the pointer of any other data-type. An example can be found here
VkShaderModule VulkanVertexArray::CreateShaderModule(const std::vector<char>& code)
{
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = code.size();
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
VkShaderModule shaderModule;
VkResult result = vkCreateShaderModule(m_device, &createInfo, nullptr, &shaderModule);
KR_CORE_ASSERT(result == VK_SUCCESS, "Failed to create shader module!");
return shaderModule;
}The createInfo.pCode pointer is of the type uint32_t. For more information see here page 106.
A crash course on difference between static_cast and dynamic_cast. dynamic_cast is a runtime cast which is used for only for polymorphic types. For example consider
class Base
{
public:
virtual void test()
{
}
};
class Child : public Base
{
public:
virtual void test() override
{
}
};
int main()
{
Base* base = new Child();
Child* ch = dynamic_cast<Child*>(base);
if (ch)
{
std::cout << "Success for dynamic_cast";
}
delete base;
}Here Child and Base are polymorphic types. Now consider the example for static_cast
class Base
{
};
class Child : public Base
{
};
int main()
{
Base* base = new Child();
Child* ch = static_cast<Child*>(base);
if (ch)
{
std::cout << "Success for static_cast";
}
delete base;
}Note that static_cast can be used for polymorphic types too. For more information read the answers here.
A crash-course on C/C++ pointer gymnastics.