Re: Problems storing objects in a vector and incrementing a static counter in C++
Bruno.DiStefano wrote:
I need to use a "vector" structure to store a number of objects that
becomes known only at run time. The constructor, at instantiation time
of a new object, increments a static counter. The value of this
counter becomes the ID of the object that has just been instantiated.
Two things:
1. Every object already has an ID that uniquely identifies it, its address.
2. You can't transfer objects into a std::vector<>, it will always be a
copying operation that creates a new ID for the newly created object.
It seems that the static counter is not accessible from within the
vector. See "4) Screen dump of execution". Actually, it seems that the
static counter is accessed once and never again.
Hmmm, my guess is that the thing described in 2. above is happening, but
without correctly creating a new ID in the copy constructor.
int MyClass::MyClassCounter=0;
MyClass::MyClass()
{
this->MyClassCounter++;
this->ID = this->MyClassCounter;
cout << endl <<"CONSTRUCTOR"<<'\t';
this->DisplayMyClassObject();
}
Several things here:
- Use initialisation instead of assignment.
- The syntax 'this->MyClassCounter' is very misleading IMHO. If you don't
like the implicit 'MyClassCounter', which is enough in memberfunctions, I'd
rather use the explicit 'MyClass::MyClassCounter' to refer to a
class-static object.
MyClass::~MyClass()
{
this->MyClassCounter--;
cout << endl <<"DESTRUCTOR"<<'\t';
this->DisplayMyClassObject();
}
Wrong. Decrementing the counter means that if you create two objects and
destroy the first of them, the next one created will have the ID of the
second one. Don't decrement the ID ever.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// MyClass.h: interface for the MyClass class.
//
#if !
defined(AFX_MYCLASS_H__CE1FCBB5_0FEC_4C67_97D2_8DF6E2694182__INCLUDED_)
#define AFX_MYCLASS_H__CE1FCBB5_0FEC_4C67_97D2_8DF6E2694182__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
Just one thing here: why did you post all that? You should have reduced the
code to a minimal example, i.e. one where you can't remove any more code,
typically only a single sourcefile. That would have made the include guards
and the (semi-portable) #pragma unnecessary.
class MyClass
{
public:
void DisplayMyClassObject(void);
MyClass();
virtual ~MyClass();
private:
int ID;
static int MyClassCounter;
};
Hmm, my assumptions were right. Search the web for "law of three" in the
context of C++. The gist is that you must control copying and assignment
for anything like that ID to work. Further, remove the explicit 'void' in
the parameterlist and the 'virtual' from the destructor. The 'void' is not
needed in C++ and the 'virtual' is unnecessary too, since this is not a
baseclass.
Uli
--
Sator Laser GmbH
Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]