Link to home
Start Free TrialLog in
Avatar of blaksaga
blaksaga

asked on

Class as an array???

I am trying to create a simple library.

class site{
 public:
  site();
  void ping();
 private:
  int interval;
  char domain[20];
};

I am going to use it to handle a bunch of domains and ping them at a given interval.  So if I wanted to handle 10 domains would I want:

site domain[10];

so then to ping one i could use

site[0].ping;

Would that be correct?  Am I even doing this right?  Sorry, I am kind of new to C++.
Avatar of blaksaga
blaksaga

ASKER

the second to last line should be

domain[0].ping;

and not

site[0].ping;

My mistake :)
hmm, you posted the question twice, might want to delete this one.

(i answered the question in the other post)
Would that be correct?  

Yes.

Am I even doing this right?  

Yes. However, you need to call ping using the () brackets. For example,

site[0].ping();

Sorry, I am kind of new to C++.

We all were at some point. :-)

Exceter
use it in second line

domain[0].ping();  //this is right approach
Yes, you can make an array.

HOwever you might even want to consider making a collectin:

class sites {
public:
   void ping();
   site & operator [] (int i);

   void add_site(.....site info...);
   void remove_site(...site key or index...);

private:
   // array or pointer to array of site objects
   // or perhaps std::vector<site>.
};

void sites::ping()
{
   for (int i = 0; i < num_sites; ++i)
     domain[i].ping();
}

site & sites::operator [] (int i)
{
   if (i < 0 || i >= num_sites)
      throw some_error;
   return domain[i];
}

Then you can have:

sites domain(10);

domain[4].ping(); // ping site 4.
domain.ping(); // ping all sites.

Another approach is to subclass std:;vector<site>:

class sites : public std::vector<site> {
public:
   void ping()
   {
      for (iterator p = begin(); p != end(); ++p)
         p -> ping();
   }
};

Then you get most of the functionality for free:

sites domain(10);
domain[3].ping(); // ping site 3
domain.ping();    // ping all

etc..

Alf
blasksaga,

why doing something which is already done for you?
use class vector from STL.

"vector" is a collection class which can help you have dynamic arrays. it will take care of memory allocations for you.

use:

#include <vector>
#include <string>

using namespace std;

typedef vector <int> INT_ARRAY;
typedef vector <string> STRING_ARRAY;

STRING_ARRAY g_YourArray;
g_YourArray.resize(10);
g_YourArray[0] = "hello";
g_YourArray[1] = "sorroy";

and so on,

codez80


If I may add.
whatever way you do it, the thing to remember - since you said you are new to c++ is to create the objects for the array.

so if you have Site Domain[10];

you need to actually allocate the objects because at this point, they are only pointers - not real living breathing objects.

ex:
    Site* pSite = new Site(//init params here maybe);
    Domain[0] = pSite;

which implies you should probably init your array too


and when you are finished playing around, don't forget to free your memory

ex:
  Site* pSite= NULL;

  for (int i = 0 ; i < 10; i++) {
    pSite = Domain[i];
    if (pSite!=NULL)  // assuming you nulled your array
       delete pSite;
  }  


hth,
Happy coding!

J
Sorry, this was a duplicate question.  An answer was accepted in the other (identical) question forum.  Thanks for your help!
If so you should post to community support and ask them refund your points and block this question.

Alf
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial