|
In the mean time you can get these semantics by butting smart pointers in the container - Boost has many such pointers.
Steve
|
|
|
|
|
std::vector and its friends in the C++ standard library will copy an added element and store the copy, not the original. std::vector doesn't destroy a contained object - it's the object destructor that handles destruction when the std::vector goes out of scope.
There's no container in the C++ standard library, which can share pointers that will be deleted when the last reference to them die. There are such alternatives in Boost[^] and Loki[^].
--
The Blog: Bits and Pieces
|
|
|
|
|
But what I expect is, when an object is inserted to a container, the container will call its copy constructor to make a copy and put into the container. Based on the result of my testing, looks like there are more than one copy of the object is created by copy constructor during the "push_back" process, and some of them got deleted, finnally only more copy remained and stored into the container. Looks like the insertion process is more complicated than just a value semantic.
|
|
|
|
|
Does this help? I suspect the additional copies may be the result of calling a function as in this example.
----------------
#include <iostream>
#include <vector>
using namespace std;
class CLogMe
{
public:
CLogMe()
{
cout << "CLogMe()" << endl;
}
CLogMe(const CLogMe &)
{
cout << "CLogMe(const CLogMe &)" << endl;
}
CLogMe& operator=(const CLogMe &)
{
cout << "operator=(const CLogMe &)" << endl;
return *this;
}
};
typedef vector<CLogMe> LogMes;
LogMes g_LogMes;
void Function(CLogMe me)
{
g_LogMes.push_back(me); // CLogMe copy constructor called here.
}
int main(int argc, char* argv[])
{
CLogMe me; // CLogMe constructor called here.
Function(me); // CLogMe copy constructor called here.
return 0;
}
Steve
|
|
|
|
|
This is exactly what I tried. I found the copy constructor get called a lot of time. After 5 push_back, copy constructor get called 20 times, and destroyed 15 times.
|
|
|
|
|
I'd say the additional copies comes from temporaries during the process of adding an element.
--
The Blog: Bits and Pieces
|
|
|
|
|
Ajax95 wrote: the container will call its copy constructor to make a copy and put into the container
The compiler, if any, makes the copy ctor call.
Ajax95 wrote: Based on the result of my testing, looks like there are more than one copy of the object is created by copy constructor during the "push_back" process
Not strange at all. The documented fact for a container with value semantics is that a copy of an element is added. It's not stipulated that one and only one copy will be made during the process and that that single copy is the one to be added. In fact, if you had taken the time to step into the push_back code while debugging, you would have noticed that lots of temporary copies are created during the process, which ensures that the original data is not tampered with.
Ajax95 wrote: some of them got deleted
Of course, that's the temporary ones that goes out of scope at each call return.
Ajax95 wrote: Looks like the insertion process is more complicated than just a value semantic.
No.
--
The Blog: Bits and Pieces
|
|
|
|
|
To avoid these extra copy constructor, call reserve function. like vec.reserve(5);
Everytime you calls push_back, vector has to expand the memory block and copies the elements from the original to new memory block. Here comes the copy constructor in picture. As the size of vector increases, the number of calls of copy constuctor keeps on increasing(directly proportional to number of elements in vector).
|
|
|
|
|
hi all
Following error occur in my atl com project.this project is using both the atl and mfc classes.Tool is visual studio 2003.
d:\Microsoft Visual Studio .NET\Vc7\atlmfc\include\atlcom.h(1759): error C2259: 'ATL::CComObject<base />' : cannot instantiate abstract class
with
[
Base=CMakeBarcode
]
Tasleem arif
|
|
|
|
|
well, once again, your question is very vague, and here, there's even no question. but i guess it is this : "Can anybody help me with this ?"
well, not really much... the compiler is already very explicit... you are creating an ATL::CComObject of a Base object, but as this Base type is an abstract class, you cannot create an instance of it.
you have two choices to solve this :
either you change the Base type and remove its "abstract" behavior, or you pass to the template parameter a child type of Base...
|
|
|
|
|
hi iwant to know where i can get the specification of ATL classes.i mean classes included in the ATL 7.
Tasleem Arif
|
|
|
|
|
|
hi all
i had made applicaton that generate the barcode and now i want the to make it as com component and acces through site. i mean online barcode geneator. it works fine as destop application.
i want to know how to use mfc classes in atl. or these allowed or cannot use these classes in atl 7. im using visual studio.net 2003. and atl 7 project.
ddd
|
|
|
|
|
cross posts are never welcomed
|
|
|
|
|
i dont think that is the cross post.is this atl forum and i want to know how to use mfc classes in atl that is simple question no cross question.
well how to read the text file in ATL i had tried to use the MFC class but it gives error when i use this when i add header file of it then it also give error.
ddd
|
|
|
|
|
do you know what cross-posting is ?
you ask on Visual C++ forum, COM forum, and now ATL forum...
i agree that your question concerns all these categories, but you have to choose the best place to ask, and wait for an answer. only then you can try another forum if you didn't get pertinent solutions...
so, i keep my position, you cross-posted the message boards
|
|
|
|
|
The most important factor is time
i m new to ATL amd COM and dont know the which section is good and secondly i dont know ur answering mechanism u visit all sections or particular. though above question was related to COM and ATL but i got my answer from MFC forum if i dont post there then i should wait the whole day perhaps whole week and so on.
Tasleem arif
-- modified at 9:00 Tuesday 28th March, 2006
|
|
|
|
|
|
Hi all,
Recently I began work localizing my apps. My source of reference is the satdll sample found in MSDN (based on MFC however).
I'm using _Module.SetResourceInstance() to point the app to my external resource dll and it seems to work as my built-in resouces are no longer used. However, the trouble is my new resources in the dll is not showing up as well (all my dialogs are skipped and message strings appear empty).
I've made sure that my resource IDs are the same but I've set the language attribute to something other than English (e.g. my french res dll will have all resources specified as French (France)).
Any advise/pointers is much appreciated.
Cheers
Sidney
--------------------------------------------
Need help with your spelling in the forum?
Check out ieSpell, spell checker addon for Internet Explorer.
|
|
|
|
|
What do you mean by "built-in resources"?
ATL 7 lets you have multiple modules that are searched for resources. If you're using VC6, you can only have one (that's what SetResourceInstance() does), so you'll need to use Win32 APIs and pass a HINSTANCE instead of using the resource loading wrappers.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
Sorry. My bad. I've managed to find my problem - it has to do with FreeLibrary getting called by a class destructor even before my dialog was displayed
On a related note, anyone have experience using InitMUILanguage to change the language of the common controls? I just tried it with the French Language ID (0x0c0c) on a US English XP but its not working. I'm wondering if its due to :
1) my system not having the French language installed? (not likely as I'm under the impression that the default US English Windows XP includes most European language support)
2) I read in a newsgroup that InitMUILanguage must be called before AtlInitCommonControls? (I can't even find AtlInitCommonControls in my code!)
Any advice is much appreciated!
--------------------------------------------
Need help with your spelling in the forum?
Check out ieSpell, spell checker addon for Internet Explorer.
|
|
|
|
|
From this blog post[^]:The ironic part is that the Windows common controls do have a workaround for this issue, even though there do not seem to be many areas of localized text in them. If you look at the topic entitled Localization Support for the Common Controls, it talks about the InitMUILanguage function (which initializes the UI language for the common controls within a process) and the GetMUILanguage function (which retrieves the UI language for the common controls within a process, or the user UI language if it has not been set). In theory there is no reason why you could not call this function at any point, but in practice it is unclear from the documentation if it will work when set multiple times (hell, I am still having trouble picturing when it works at all, since I cannot think of any localized text!).
--Mike--
Visual C++ MVP
LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
I export a class in dll examples
typedef vector<string> strvector;
class __declspec(dllexport) myclass()
{
void setvector(strvector &list);
}
void setvector(strvector &list)
{
list.push_back("mystring");//change the data
}
if i use this dll in my program
myclass mydll;
strvector mylist;
mydll.setvector(mylist);
if i use mylist again my program will crash;
how to deal with that?
i need to change the vector in my dll;
thanks thanks a lot.
|
|
|
|
|
You wrote:
typedef vector<string> strvector;
//just to make it clear to the others.
From what I have heard, it is not possible to pass stl data types like string , to dlls. You can use the good old character array etc.
this is this.
|
|
|
|
|
It is possible. Although, you'll have to do some export magic.
(I *think* Microsoft has already provided the magic needed for std::string)
|
|
|
|