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;
}
//***********************************************************************************************

You can't do that.. I cannot remember why but I remember ancient dragon telling me that it's a no-no. Something to do with pointers if I remember correctly.. Instead I was told to make a tracker variable and everytime you add to the array, increment the tracker by that amount. Else use std::vector instead. I'd assume the same applies to you unless of course I'm wrong.

I don't see where BuildingsArr is declared. If its a pointer, sizeof won't give you what you are lookinig for. sizeof gives you the number of bytes consumed in memory for the object. For pointers, it will be 4 because sizeof(any pointer) is 4.

Thanks for the replies, i did forget to post the declaration of BuildingsArr, it is as follows

PublicBuilding* BuildingsArr[]

How is the best way to achieve what i need without the use of a new variable as that is not allowed in the program task?

I don't think you can without creating a new variable aka a tracker variable. It can even be implemented in your class I suppose. The only other way " I " can possibly think of is to do what vectors do and keep a pointer to the beginning of the array and one to the end.. then minus them for the size and capacity.. but that's far from what you want and I suggest just changing the arrays to vectors. Vectors come with a .Size() method to keep track automatically for you..

If you cannot do that then I guess you can view this thread and gather what you can from it: http://www.gamedev.net/topic/345898-finding-array-length----c/

Even though I doubt it could be useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.