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.e nd());
}
};
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. 1 1822
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.e nd());
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 * operator(unsigned int index);
};
|
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 or
not. My first idea, using the const and non-const versions of
operator, was clearly not correct, as was pointed out. Julián Albo
suggested I could use proxies to do that. I've done some googling for
proxies (also in this group) and personally,...
|
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;
Matrix() {}
|
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) {return
|
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 to do this is
because it enables me to apply some functions of a vector to a row of
of matrix, e.g., I have a function to compute the sum of vector:
double my_sum(vector<double> x)
{
| |
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 like to be able to
modify this private array in the class with the operator . I know
that the operator can be overloaded. However, is there away to overload
?
Thanks,
Michael
|
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 {
|
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 assignment is done of the
referenced OtherObj object (according to the compiler messages). This is
strange, since the referenced OtherObj is initialized using member
initialization lists. Anyone a clue?
thanks,
|
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 v);
|
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
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |