add pointer

add "dynamic" heap array
add polymorphic
This commit is contained in:
2025-03-26 12:44:05 +01:00
parent abb1ea4a38
commit 4b6ac6a484
13 changed files with 339 additions and 1 deletions
+14
View File
@@ -11,6 +11,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Classes", "Classes\Classes.
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Collections", "Collections\Collections.vcxproj", "{1B177393-37B1-4C7B-BCF0-163991832930}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Collections", "Collections\Collections.vcxproj", "{1B177393-37B1-4C7B-BCF0-163991832930}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Pointer", "Pointer\Pointer.vcxproj", "{860DAD08-212F-4265-B3F8-8BBF85AA4833}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64 Debug|ARM64 = Debug|ARM64
@@ -69,6 +71,18 @@ Global
{1B177393-37B1-4C7B-BCF0-163991832930}.Release|x64.Build.0 = Release|x64 {1B177393-37B1-4C7B-BCF0-163991832930}.Release|x64.Build.0 = Release|x64
{1B177393-37B1-4C7B-BCF0-163991832930}.Release|x86.ActiveCfg = Release|Win32 {1B177393-37B1-4C7B-BCF0-163991832930}.Release|x86.ActiveCfg = Release|Win32
{1B177393-37B1-4C7B-BCF0-163991832930}.Release|x86.Build.0 = Release|Win32 {1B177393-37B1-4C7B-BCF0-163991832930}.Release|x86.Build.0 = Release|Win32
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Debug|ARM64.ActiveCfg = Debug|ARM64
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Debug|ARM64.Build.0 = Debug|ARM64
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Debug|x64.ActiveCfg = Debug|x64
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Debug|x64.Build.0 = Debug|x64
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Debug|x86.ActiveCfg = Debug|Win32
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Debug|x86.Build.0 = Debug|Win32
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Release|ARM64.ActiveCfg = Release|ARM64
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Release|ARM64.Build.0 = Release|ARM64
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Release|x64.ActiveCfg = Release|x64
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Release|x64.Build.0 = Release|x64
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Release|x86.ActiveCfg = Release|Win32
{860DAD08-212F-4265-B3F8-8BBF85AA4833}.Release|x86.Build.0 = Release|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
+4
View File
@@ -11,6 +11,10 @@ Entity::Entity(std::string name) : name(name)
//this->name = name; //this->name = name;
} }
Entity::~Entity()
{
}
std::string Entity::ToString() std::string Entity::ToString()
{ {
return name; return name;
+1
View File
@@ -6,6 +6,7 @@ class Entity
public: public:
Entity(); Entity();
Entity(std::string name); Entity(std::string name);
virtual ~Entity();
virtual std::string ToString(); virtual std::string ToString();
+2
View File
@@ -3,5 +3,7 @@
class ICompareable class ICompareable
{ {
public: public:
virtual ~ICompareable() {}
virtual int Compare(ICompareable* other) = 0; virtual int Compare(ICompareable* other) = 0;
}; };
+4
View File
@@ -7,6 +7,10 @@ Monster::Monster(int strength) : Entity("My Monster"), _strength(strength)
this->name = "My Monster"; this->name = "My Monster";
} }
Monster::~Monster()
{
}
//Monster Monster::Create(Monsters type) //Monster Monster::Create(Monsters type)
//{ //{
// Monster monster; // Monster monster;
+1
View File
@@ -13,6 +13,7 @@ class Monster : public Entity, public ICompareable
{ {
public: public:
Monster(int strength); Monster(int strength);
virtual ~Monster();
//static Monster Create(Monsters type); //static Monster Create(Monsters type);
+10
View File
@@ -6,6 +6,16 @@ Ork::Ork() : Monster(10)
this->name = "My Ork"; this->name = "My Ork";
} }
Ork::Ork(const Ork& other) : Monster(other._strength)
{
this->name = other.name;
this->_speed = other._speed;
}
Ork::~Ork()
{
}
void Ork::Move() void Ork::Move()
{ {
} }
+3 -1
View File
@@ -4,7 +4,9 @@
class Ork : public Monster class Ork : public Monster
{ {
public: public:
Ork(); Ork(); // default constructor
Ork(const Ork&); // copy constructor
~Ork(); // destructor
void Move() override; void Move() override;
}; };
+17
View File
@@ -24,5 +24,22 @@ int main()
Vector3 position(1.0f, 3.0f, -5.0f); Vector3 position(1.0f, 3.0f, -5.0f);
position.y = 0.0f; position.y = 0.0f;
ICompareable* pComparable = nullptr;
pComparable = &ork;
pComparable->Compare(&ork);
(*pComparable).Compare(&ork); // same as line above, but more complicated to write
ICompareable* pComparables[] = { new Ork, new Ork, new Ork };
for (size_t i = 0; i < size(pComparables); i++)
{
//pComparables[i]->Compare(nullptr);
delete pComparables[i];
pComparables[i] = nullptr;
}
ICompareable** pHeapComparables = new ICompareable*[10];
delete[] pHeapComparables;
pHeapComparables = nullptr;
return 0; return 0;
} }
+13
View File
@@ -51,6 +51,19 @@ int main()
printArray(numbers2); printArray(numbers2);
printArray(numbers2, std::size(numbers2)); printArray(numbers2, std::size(numbers2));
// "dynamic" (heap) arrays
int count2 = 10;
int* heapNumbers = new int[count2];
heapNumbers[0] = 10;
int* pHeapNumber = heapNumbers;
for (size_t i = 0; i < count2; i++)
{
cout << pHeapNumber << ": " << *pHeapNumber << " ";
pHeapNumber++;
}
delete[] heapNumbers; // CAUTION - heap arrays have to be deleted with delete[] not delete
heapNumbers = nullptr;
// stl collections // stl collections
vector<int> numbers6; vector<int> numbers6;
vector<int> numbers7 = { 0, 1, 2, 3 }; vector<int> numbers7 = { 0, 1, 2, 3 };
+190
View File
@@ -0,0 +1,190 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|ARM64">
<Configuration>Debug</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ARM64">
<Configuration>Release</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{860dad08-212f-4265-b3f8-8bbf85aa4833}</ProjectGuid>
<RootNamespace>Pointer</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
+58
View File
@@ -0,0 +1,58 @@
#include <iostream>
using namespace std;
void changeInteger(int n);
void changeIntegerPerPointer(int* pN);
int changeIntegerPerReturn(int n);
void changeIntegerPerReference(int& n);
int main()
{
// general
int* pNumber = nullptr;
pNumber = new int(0);
cout << "size of pNumber: " << sizeof(pNumber) << endl;
cout << "size of pNumber data: " << sizeof(*pNumber) << endl;
*pNumber = 567;
//pNumber = new int(0); // BAD BAD - Memory Leak, because we override an existing pointer address with a new one without saving the old one
// here are 100s of lines of code
if (pNumber != nullptr)
{
delete pNumber;
pNumber = nullptr;
}
// pointer as references
int number = 0;
changeInteger(number);
changeIntegerPerPointer(&number);
changeIntegerPerPointer(pNumber);
changeIntegerPerReference(number);
return 0;
}
void changeInteger(int n)
{
n = 100;
}
void changeIntegerPerPointer(int* pN)
{
cout << "address of pN: " << &pN << endl;
if (pN == nullptr) return; // BE CAREFUL - if you don't what you get, do a test before
*pN = 200;
}
void changeIntegerPerReference(int& n)
{
n = 300;
}