473,320 Members | 2,020 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,320 software developers and data experts.

how to operator a stl vector pointer?

Dear all,

Here is my code.
------------------------------
#include <iostream>
#include <vector>
using namespace std;

class A {
public:
int a;
A() { a=0; }
};

class B {
public:
vector<A*> vecA;
B() {}
~B() {
for (int i=0;i<vecA.size();i++) {
if (vecA[i]) {
delete vecA[i];
vecA[i]=NULL;
}
}
vecA.erase(vecA.begin(),vecA.end());
}
};

int main()
{
vector<B*>* BB=new vector<B*>;
// How to get or assign a value of class A object?
return 0;
}
---------------------------------------

How to get or assign a value of class A object?
Thanks for your help.

Regards,
cylin.

Jul 22 '05 #1
1 1781
cylin wrote:
Dear all,

Here is my code.
------------------------------
#include <iostream>
#include <vector>
using namespace std;

class A {
public:
int a;
A() { a=0; }
You should use initializer lists instead:

A() : a(0) {}

Doesn't make much of a difference for builtin types, but for classes, it
does. So it's better to get used to that.
};

class B {
public:
vector<A*> vecA;
B() {}
~B() {
for (int i=0;i<vecA.size();i++) {
if (vecA[i]) {
delete vecA[i];
vecA[i]=NULL;
}
}
vecA.erase(vecA.begin(),vecA.end());
No need for that erase(). First of all, there is a clear() member
function, but besides that, a vector is cleared on destruction anyway.
}
};

int main()
{
vector<B*>* BB=new vector<B*>;
Why are you using so much pointer stuff? Especially, why are you
dynamically allocating your vector? This makes things a lot more
complicated than necessary.
// How to get or assign a value of class A object?
you mean you want to access an element of a B's vector of A through an
element of your vector of B's, to which you have a pointer? Sounds way
too complicated, but here you go:

(*BB)[3]->vecA[5]->a = 3;
this will assign 3 to the 'a' member of the fifth element of the 'vecA'
member of the 3rd element of your B vector through the BB pointer.

But again, your design is much too complicated, and you don't take real
advantage of vectors or any OO concepts.
return 0;
}
---------------------------------------

How to get or assign a value of class A object?
Thanks for your help.

Regards,
cylin.


Jul 22 '05 #2

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

Similar topics

4
by: Thomas Matthews | last post by:
Hi, I have a class Record which is a container of fields. I have overloaded operator to return a pointer to a Field: class Field; // Forward declaration class Record { public: Field * ...
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
47
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) ...
7
by: check.checkta | last post by:
Hi, I'd like to implement a simple matrix class. I'd like to overload operator so that it returns as a vector (either the stl vector or some other Vector class of my own). The reason I want...
6
by: Michael DeWulf | last post by:
I am trying to make a 2D matrix class. The data in the matrix will be of type int and so the underlying data structure will be a 2D array (int ** matrix). To make the data easy to modify, I would...
3
by: Kush | last post by:
Hello All, Once again another question. :) This time I would like to understand why one needs to overload the operator to access objects in array format? Example code: class A {
3
by: Bram Kuijper | last post by:
Hi all, I am trying to resize a vector of objects (MyObj below), which contain references to other objects (OtherObj, see below). However, apparently somewhere in the resize operation an...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
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...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.