?
Solved

Class as an array???

Posted on 2003-02-22
10
Medium Priority
?
246 Views
Last Modified: 2013-12-14
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++.
0
Comment
Question by:blaksaga
10 Comments
 

Author Comment

by:blaksaga
ID: 8001914
the second to last line should be

domain[0].ping;

and not

site[0].ping;

My mistake :)
0
 

Expert Comment

by:Fallen_Knight
ID: 8001917
hmm, you posted the question twice, might want to delete this one.

(i answered the question in the other post)
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8001918
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Expert Comment

by:manu2020
ID: 8002443
use it in second line

domain[0].ping();  //this is right approach
0
 
LVL 12

Expert Comment

by:Salte
ID: 8002854
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
0
 
LVL 1

Expert Comment

by:codez80
ID: 8003654
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

0
 
LVL 2

Expert Comment

by:BorlandMan
ID: 8014323

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
0
 

Author Comment

by:blaksaga
ID: 8420933
Sorry, this was a duplicate question.  An answer was accepted in the other (identical) question forum.  Thanks for your help!
0
 
LVL 12

Expert Comment

by:Salte
ID: 8421163
If so you should post to community support and ask them refund your points and block this question.

Alf
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 8422458
PAQ'd and 50 points refunded

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question