473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to do this better?

I have a class that wants to keep track of all instances of itself.
So, I'm currently doing something like this:

class X
{
public:
X ( int blah );
const int blah();
~X();

private:
X();
X ( const X& rhs );
const X& operator= ( const X& rhs );
static vector<X *> *xList;
int blah_;
};

The private constructors and the operator= are not defined, so the
only way to make one is via the public constructor (right?).

The xList pointer is initially zero.

Whenever one of these is created, the constructor checks whether the
xList pointer is still zero, and if so, initializes it to a new
vector<X *>. It then pushes "this" onto xList..

Whenever one is destroyed, the destructor finds "this" on xList, and
erases it; if xList is then empty, it also does a delete on xList, and
sets xList back to zero.

So, my first question: Is there anything explicitly wrong with the
above?

Second: If nothing's outright wrong with it, is there a better way to
do it?

Now, the next thing is that I want the outside world to be able to
look at the list of existing x's - but not modify it. So, I defined a
couple of public static functions:

const vector<X *>::size_type getXCount();
const X& getX ( vector<X *>::size_type index );

If xList is zero, getXCount returns zero, else it returns
xList->size(). getX gives a const reference to the X at the desired
index (initializing xList first if it is zero) by using xList->at().
So it's going to throw some exception if an invalid index is
specified.

Third question: Anything wrong with that?

Fourth question: Is there a better way?

Really what I would like is to just let the outside world use standard
vector stuff to look through the list, but I do not want them to be
able to modify the list. So, for example, I would like them to be
able to begin(), find(), operator++, and so forth, but not
push_back(), erase(), and so forth. I could define all of these
functions statically in my X class, but is there a less verbose way?
Like (I don't know what I'm talking about here) a public function that
returns a const reference to the vector, or something?

Thanks in advance for any help.

Bob Vesterman.
Jul 19 '05 #1
3 1691
"Robert William Vesterman" <bo*@vesterman.com> wrote...
I have a class that wants to keep track of all instances of itself.
So, I'm currently doing something like this:

class X
{
public:
X ( int blah );
const int blah();
~X();

private:
X();
X ( const X& rhs );
const X& operator= ( const X& rhs );
static vector<X *> *xList;
I would use 'list' or 'set'. And there is no need to have it
as a pointer. Would eliminate the need to check it.
int blah_;
};

The private constructors and the operator= are not defined, so the
only way to make one is via the public constructor (right?).

The xList pointer is initially zero.

Whenever one of these is created, the constructor checks whether the
xList pointer is still zero, and if so, initializes it to a new
vector<X *>. It then pushes "this" onto xList..

If you don't declare it a pointer, there is no need to check,
just push.

Whenever one is destroyed, the destructor finds "this" on xList, and
erases it; if xList is then empty, it also does a delete on xList, and
sets xList back to zero.

So, my first question: Is there anything explicitly wrong with the
above?
No.

Second: If nothing's outright wrong with it, is there a better way to
do it?
Use 'list' or 'set'. They are faster WRT insertions/deletions
than a 'vector'.

Now, the next thing is that I want the outside world to be able to
look at the list of existing x's - but not modify it. So, I defined a
couple of public static functions:

const vector<X *>::size_type getXCount();
const X& getX ( vector<X *>::size_type index );
Both those members should probably be either static or const.

If xList is zero, getXCount returns zero, else it returns
xList->size(). getX gives a const reference to the X at the desired
index (initializing xList first if it is zero) by using xList->at().
If you define it as 'list', the order is preserved (just like in
a vector, but getting one from a particular index could be slower.
With a 'set' the creation (insertion) order is not preserved, and
getting one from a particular index is slow too. So, pick the
collection based on your requirements.
So it's going to throw some exception if an invalid index is
specified.

Third question: Anything wrong with that?
If the order is important, forget I suggested 'set'.

Fourth question: Is there a better way?
Naming or keeping an ID could be the next step. Then you might
want to look at 'map' as your container.

Really what I would like is to just let the outside world use standard
vector stuff to look through the list, but I do not want them to be
able to modify the list. So, for example, I would like them to be
able to begin(), find(), operator++, and so forth, but not
push_back(), erase(), and so forth. I could define all of these
functions statically in my X class, but is there a less verbose way?
Like (I don't know what I'm talking about here) a public function that
returns a const reference to the vector, or something?


Sure, you could do that.

operator const vector<X*> () const { return xList; }

(again, I don't think there is a need to have 'xList' as a pointer)

Victor
Jul 19 '05 #2
On Wed, 25 Jun 2003 19:58:38 -0400, "Victor Bazarov"
<v.********@attAbi.com> wrote:
"Robert William Vesterman" <bo*@vesterman.com> wrote...

Really what I would like is to just let the outside world use standard
vector stuff to look through the list, but I do not want them to be
able to modify the list. So, for example, I would like them to be
able to begin(), find(), operator++, and so forth, but not
push_back(), erase(), and so forth. I could define all of these
functions statically in my X class, but is there a less verbose way?
Like (I don't know what I'm talking about here) a public function that
returns a const reference to the vector, or something?


Sure, you could do that.

operator const vector<X*> () const { return xList; }


I am confused by this syntax. Could you please show how a client
class would use this to get the xList from X?

Also, am I correct in the following? The reason why the above would
not allow them to do (for example) a push_back() is because the vector
they're given is const, and push_back() is not a const function, and
is therefore not allowed on const objects?

Thanks,

Bob Vesterman.
Jul 19 '05 #3
"Robert William Vesterman" <bo*@vesterman.com> wrote in message
news:55********************************@4ax.com...
On Wed, 25 Jun 2003 19:58:38 -0400, "Victor Bazarov"
<v.********@attAbi.com> wrote:
"Robert William Vesterman" <bo*@vesterman.com> wrote...

Really what I would like is to just let the outside world use standard
vector stuff to look through the list, but I do not want them to be
able to modify the list. So, for example, I would like them to be
able to begin(), find(), operator++, and so forth, but not
push_back(), erase(), and so forth. I could define all of these
functions statically in my X class, but is there a less verbose way?
Like (I don't know what I'm talking about here) a public function that
returns a const reference to the vector, or something?


Sure, you could do that.

operator const vector<X*> () const { return xList; }


I am confused by this syntax. Could you please show how a client
class would use this to get the xList from X?

Also, am I correct in the following? The reason why the above would
not allow them to do (for example) a push_back() is because the vector
they're given is const, and push_back() is not a const function, and
is therefore not allowed on const objects?


First, let me say that I made a mistake. I intended to write

operator const vector<X*>& () const { return xList; }

because there is no need to copy the stuff, returning a reference
should be satisfactory.

As to how to use it, this should be an illustration:

const vector<X*>& v(yourClassInstance);

and then do what they want, like accessing v[i] or v.begin()
or whatever. And yes, you're correct, since the vector is const
they won't be able to modify it directly (without a const_cast).

Victor
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
3
by: Muhd | last post by:
<usualDisclaimer>Please forgive me if this is in the wrong group, and if so, what is the right group.</usualDisclaimer> Let me start off by first saying im a newb. Ok, with that out of the way I...
24
by: Faith Dorell | last post by:
I really don´t like C.You can write better programs in BASIC than in C, if you don´t like this language. I don´t understand how C became so popular, although much better programming languages...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
33
by: Protoman | last post by:
Which is better for general-purpose programming, C or C++? My friend says C++, but I'm not sure. Please enlighten me. Thanks!!!!!
22
by: JoeC | last post by:
I am working on another game project and it is comming along. It is an improvment over a previous version I wrote. I am trying to write better programs and often wonder how to get better at...
19
by: Alexandre Badez | last post by:
I'm just wondering, if I could write a in a "better" way this code lMandatory = lOptional = for arg in cls.dArguments: if arg is True: lMandatory.append(arg) else: lOptional.append(arg)...
23
by: mike3 | last post by:
Hi. (posted to both newsgroups since I was not sure of which would be appropriate for this question or how specific to the given language it is. If one of them is inappropriate, just don't send...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
3
by: Ryan Liu | last post by:
Hi, Is Async I/O (e.g. NetworkStream.Begin/End Read/Write) always better than synchronous I/O? At least as good? When I don't concern about easy or difficult to write code, should I always...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.