I am trying to use the size of an array to determine how many entrys are in the array as follows
sizeof(array)/sizeof(array type) = number of elements in the array.
I then wish to use this to add more data to the array as follows
array[sizeof(array)/sizeof(array type)] = new Object(object parameters);
The problem I have is that the size of array always returns 0
I have attached the code i believe necessary, could someone please explain why this is not working and how to make somethign equivalent work.
Thanks in advance
smurfVillage.h
#ifndef smurfVillage_H
#define smurfVillage_H
#include <string>
using namespace std;
class GenericBuilding
{
private:
string building_name;
string location;
string functionality;
public:
GenericBuilding();
GenericBuilding(string,string,string);
~GenericBuilding();
string getBuildingName();
string getLocation();
string getFunctionality();
void setBuildingName(string);
void setLocation(string);
void setFunctionality(string);
};
class PublicBuilding: public GenericBuilding
{
public:
PublicBuilding();
PublicBuilding(string,string,string);
~PublicBuilding();
void executeFunctionality(string);
};
#endif
smurfVillage.cpp
#include "smurfVillage.h"
#include <iostream>
#include <string>
using namespace std;
//***********************GenericBuilding Constructors ***********************//
GenericVillage::GenericVillage()
{
//default parameterless constructor
std::cout << " >> Constructing GenericVillage" << std::endl;
std::cout << std::endl;
}
//*******************End of GenericVillage Constructors ********************//
//************************GenericVillage Destructor ************************//
GenericVillage::~GenericVillage()
{
std::cout << " >> Destructing GenericVillage" << std::endl;
std::cout << std::endl;
}
//********************End of GenericVillage Destructor *********************//
//**************************************************************************************************
void GenericVillage::constructBuildings(string Name, string Location, string Functionality)
{
Heres the problem
std::cout << sizeof(BuildingsArr)/4 << std::endl;
BuildingsArr[sizeof(BuildingsArr)/4] = new PublicBuilding(Name, Location, Functionality);
std::cout << sizeof(BuildingsArr)/4 << std::endl;
}
void GenericVillage::population()
{
std::cout << "Total population in village is: " << sizeof(CreaturesArr)/4 << std::endl; //divide by 4 because of ptr
std::cout << std::endl;
}
void GenericVillage::numberOfBuildings()
{
std::cout << "Total number of buildings in village is: " << sizeof(BuildingsArr)/4 << std::endl; //divide by 4 because of size of ptr
std::cout << std::endl;
}
//***********************************************************************************************