"anand" <writeanand@gmail.com> wrote in message
news:1117219549.860290.271610@o13g2000cwo.googlegr oups.com...[color=blue]
>I am an STL newbie trying to build a class DUTBus that has a map
> object. In my member function, I try to return a Map Object, however,
> it does not seem to work. I am referring to "Bus getBus()"
> function.
>
> I need to do this because in order to "add" device pins (via
> registerPin() function. In other words, I need to modify the map by
> inserting new (key,value) pairs into the object. However, when I try to
> access the bus object by returning it, it doesnt work!
>
> Please help!
>
> Thanks
> Anand
>
> #include <string>
> typedef map<int, string> Bus;
> typedef Bus::iterator BusIterator;
>
> class DUTBus
> {
> private:
> Bus _bus;[/color]
NB: Leading underscores are best avoided in identifier names
(they are reserved for the implementation in some contexts).
Trailing underscores (bus_) are therefore usually preferred.
[color=blue]
> public:
> DUTBus() { }
> Bus getBus();[/color]
Problem: In C++, this returns a (deep) copy of the stored _bus
object. If you want to return an object reference, you need to
do so explicitly:
Bus& getBus() { return _bus; }
However, the latter defeats the purpose of having a private
data member, and is therefore best avoided.
A sometimes useful alternative, which still allows the class to
control its invariants, is:
Bus const& getBus() const { return _bus; }
[color=blue]
> inline int getBusSize() { return (_bus.size()); }[/color]
NB: inline is redundant here, and a 'const' would be better:
int getBusSize() const { return (_bus.size()); }
[color=blue]
> void registerPin(int, string);
>
> };
>
>
> Bus DUTBus::getBus()
> {
> return(_bus);
> }
>
> void DUTBus::registerPin(int _idx, string _pin)\[/color]
Again, leading underscores are best avoided. And it is
a quite uncommon C++ style to use them in this context.
[color=blue]
> {
> Bus myBus = this->getBus();[/color]
This creates a local copy of the _bus object. That local
copy is then modified, and destroyed, while the _bus data
member remains unchanged. ***this is what causes your 'bug'***
Use the _bus data member direcly. Or if you want a local
reference to the data member, use:
Bus& myBus = _bus;
[color=blue]
> BusIterator iter;
> iter = myBus.begin();[/color]
Prefer combining declaration and instanciation:
BusIterator const iter = myBus.begin();
But anyway, 'iter' is not needed in this function.
[color=blue]
> myBus.insert(pair<int, string>(_idx,_pin));
> cout << "Inserted Bus[" << _idx << "]\t=\t" << _pin;
> }[/color]
The function will work correctly if you replace its body with:
_bus.insert( pair<int, string>(_idx,_pin) );
Or even easier, since you don't seem to care about wheter a pin
with the same number already existed:
_bus[_idx] = _pin;
[color=blue]
> main(void)
> {
> Bus myBus;[/color]
It is better to declare local variables on first use...[color=blue]
> DUTBus* DataBus = new DUTBus();[/color]
Don't allocate objects on the heap unless it is necessary. Just use:
DUTBus DataBus;[color=blue]
> DataBus->registerPin(1,"T_x_p_ad1");
> DataBus->registerPin(2,"T_x_p_ad2");
> DataBus->registerPin(3,"T_x_p_ad3");
>
> myBus = DataBus->getBus();[/color]
This again creates a local *copy* of the DataBus->_bus , which
may not be what you want (although it will work ok in this context).
[color=blue]
> cout << "This bus has a size of " << myBus.getBusSize() << endl;
> }[/color]
I hope this helps. Note that it might be a good idea to spend more time
studying C++ before trying to write more complex programs...
Regards, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <>
http://www.brainbench.com
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]