Is it possible to assign function parameters directly into a vector holding a structure instead of declaring a local struct variable?
Below is sort of what I'm wanting to do; the following is declared as private data in a class -
typedef struct
-
{
-
std::string s1;
-
std::string s2;
-
int i1;
-
int i2;
-
} item;
-
-
std::vector<item> *items;
-
I've got a member function dataAddItem() -
bool className::dataAddItem(
-
const std::string &s1,
-
const std::string &s2,
-
const int &i1,
-
const int &i2)
-
{
-
-
// items->push_back(struct) ??
-
}
-
I'm wanting to assign the parameters without having to declare a temporary structure. Is this possible?
TIA
6 11462
No. Since you have a vector<item> you have to push_back and item.
However, once the item is in the vector then you can:
This violates encapsulation but it will work.
Don't worry about temporary copies until your code works. Then you can go through it and use references where possible. After which you replace any remaining pointers with smart poiters. However, just using references will remove 90% of your pointers.
I am not sure why your items is a pointer vector<item>. Are you making assumptions abut how vector is implemented? I can guarantee it's not on the stack.
Thank you for your reply.
I'm finding that things I'm somewhat familiar with in C go horribly wrong in C++.
I found a bit of code that *appears* to do what I'd like but I don't know if it's valid. My knowledge of C++ is solely based on snippits of code found in forums and whatnot. -
struct item
-
{
-
...
-
item(const string &p1, const string &p2, ...) :
-
s1(p1), s2(p2), ... ) {}
-
-
}; // handy feature but bizzare compared to C
-
-
bool ::dataAddItem(param list)
-
{
-
items->push_back(item(param list));
-
// use a constructor within a struct?? weird
-
}
this compiles and runs without errors, but is it valid?
With C, I was told it's better to use a pointer to a structure or large data, so I tried the same approach with the vector.
Please forget C. C is the reason there is a C++.
C++ Secret: There are no classes in C++.
In C the only way you can have a data item with members is to use a struct. Therefore, in C++ you use a struct to encapsulate members since C++ extends C.
C++ uses constructors to initialize objects. Therefore, structs have constructors. Ditto for destructors. Member functions, etc...
If you define every member of a struct as public, private or protected, then there is no difference between a struct and a class.
C++ Secret #2: The "class" keyword is there to make object-oriented programmers think that C++ is object-oriented, but it's not really.
In the OOP world a class is a pattern for an object. So you say an object is an instance of a class. The OOP world never heard of a struct since it is C language artifact. By implementing a "class" as a struct, C++ bridges C to OOP. If you can use a struct you automatically can use a class.
Your example code just shows me a struct with a constructor.
Once again, I cannot thank you enough taking the time to explain things.
Here's a compacted version of what I'm working with -
-
class test
-
{
-
private :
-
-
struct item
-
{
-
string s1;
-
string s2;
-
item(const string &init_data1,
-
const string &init_data2) :
-
s1(init_data1), s2(init_data2) {}
-
};
-
-
vector<item> items;
-
vector<item>::iterator it;
-
-
public : bool dataAddItem(const string &);
-
void dataShowItems();
-
};
-
-
bool test::dataAddItem(const string ¶m1,
-
const string ¶m2)
-
{
-
items.push_back( item(param1, param2) );
-
}
-
-
void test::dataShowItems()
-
{
-
for(it = items.begin(); it != items.end(); ++it)
-
cout << it->s1
-
<< "\n"
-
<< it->s2
-
<< "\n\n";
-
}
-
-
test obj;
-
obj.dataAddItem("s1 data", "s2 data");
-
obj.dataShowItems();
-
-
// output
-
// s1 data
-
// s2 data
-
What I don't quite get with dataAddItem is how push_back is able to use item's constructor so that "s1 data" and "s2 data" are added to the vector.
Here's the line of code: - items.push_back( item(param1, param2) );
So you are calling an item constructor as the argument to push_back. To make that call, the compiler creates an item object and calls the construtor on it. Now param1 and param2 are inside this hidden (temporary) item object. This temporary object is passed to push_back which adds a copy of it (copy constructor call) to the vector and finally as the temporary goes out of scope, the item destructor is called.
In C you would need to make all these calls yourself (if you remembered) but in C++ the compiler creates, initializes and cleans up the object withut bothering you. This drives C programmers nuts since they want total control of their program. None of this automatic protection stuff.
Thank you; I appreciate your patience and your comments have been extremely helpful.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Anonymous |
last post by:
Is there a non-brute force method of doing this?
transform() looked likely but had no predefined function object.
std::vector<double> src;
std::vector<int> dest;
...
|
by: ali labed |
last post by:
Does MS Visual C++ not accept vector of a structure ? (in acceptes
vector<int>)
struct str1
{
string name;
int num;
};
vector<str1> vec_str( 10 ); // <== rejected
|
by: sharmadeep1980 |
last post by:
Hi All,
I am facing a very unique problem while compling my project in
"Release" build.
The project is building in DEBUG mode but giving linking error on
Release build.
Here is the error:...
|
by: aaragon |
last post by:
I am trying to create a vector of type T and everything goes fine until
I try to iterate over it. For some reason, the compiler gives me an
error when I declare
std::vector<T>::iterator iter;...
|
by: arnuld |
last post by:
in C++ Primer 4/3 Lippman says in chapter 3, section 3.3.1:
vector<stringsvec(10);
// 10 elements, each an empty string
here is the the code output & output from my Debian box running "gcc...
| |
by: Lionel B |
last post by:
On my platform I find that the std::vector<boolspecialisation incurs a
significant performance hit in some circumstances (when compared, say, to
std::vector<intprogrammed analagously). Is it...
|
by: stinos |
last post by:
Hi All!
suppose a class having a function for outputting data somehow,
class X
{
template< class tType >
void Output( const tType& arg )
{
//default ToString handles integers/doubles
|
by: barcaroller |
last post by:
I have a pointer to a memory block. Is there a way I can map a vector<Tto
this memory block? I would like to take advantage of the powerful vector<T>
member functions. Please note that I cannot...
|
by: Rockair |
last post by:
hi!
there is a class:
class card
{
static vector<string> names;
//...
};
|
by: jacek.dziedzic |
last post by:
Hi!
I need to be able to track memory usage in a medium-sized
application I'm developing. The only significant (memory-wise) non-
local objects are of two types -- std::vector<and of a custom...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |