|
This is a technicality, but it might be worth mentioning:
string s1 = "the truth is out there"; You said that this sentence involves the default ctor and the assignment operation. In fact, none of these two is called here. The sequence is:- A temporary object is constructed from
"the truth is out there" using string::string(const char *) .
s1 is constructed from this temporary object using the copy ctor. The compiler is allowed to optimize away the temporary and the call to the copy ctor, as in fact VC++ does*. So, for our particular scenario, only string::string(const char *) is involved in the sentence.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
* The thing is rather complicated: for the optimization to be allowed it is necessary that the copy ctor be accesible (i.e. not private ). VC++, however, only conforms to this rule if MS proprietary extensions to the language are disabled (/Za).
|
|
|
|
|
Because string has no copy constructor, when the donothing1 invoked, compiler will create a temporary object, and invoke the default copy contructor, so, the temporary object will copy the point "data" from localstring, and when the donothing1 is finished, the temporary object will destoried, and the infomation stored in "data" will lost. That's the problem I read from <<effective c++="">>, but what happen when invoke donothing2 function?
I'm amumu, and you?
|
|
|
|
|
I just looked at your code from earlier, you have not invoked the donothing2 function.
qf0421 wrote:
<br />
main()<br />
{<br />
string s1 = "the truth is out there";<br />
<br />
donothing1(s1);<br />
<br />
string s2 = "the truth is out there, too";<br />
<br />
donothing1(s2);<br />
<br />
}<br />
|
|
|
|
|
As donothing2 accepts a reference, there's no copying involved in the calling process, and no problem as those posed by the first function will arise. If you think of a reference (in this context) as an alias for a pointer you'll see more clearly the differences between donothing1 and donothing2 .
Nevertheless, it is very much advisable to explictly write the "gang of four" (default ctor, copy ctor, assignment operator and dtor) for every class wihtout trivial semantics and not leave these holes to the interpretation and good sense of the user.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joaquín M López Muñoz wrote:
A temporary object is constructed from "the truth is out there" using string::string(const char *).
s1 is constructed from this temporary object using the copy ctor.
That is incorrect. The following two lines are exactly the same:
string s1 = "the truth is out there";
string s1 ("the truth is out there"); They both call the one-parameter constructor.
--Mike--
"There are three things I can never remember, 1: um... 2: um... 3: um..."
-- Victor Borge
My really out-of-date homepage
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan and Jamie Salé.
|
|
|
|
|
That is incorrect. The following two lines are exactly the same
No they are not. My post explains it in pretty much detail.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
*shrug* Try it yourself. 'string s = "abc"' has nothing to do with the copy constructor or the = operator.
--Mike--
"There are three things I can never remember, 1: um... 2: um... 3: um..."
-- Victor Borge
My really out-of-date homepage
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan and Jamie Salé.
|
|
|
|
|
I did! Try the following (please bare with typos, I'm writing this without IDE support):
class A
{
public:
A(const char *)
{
}
private:
A(const A&);
};
int main()
{
A a="hello";
return 0;
} Under default settings, whis will compile fine and actually only A::A(const char*) is being invoked. However, if you set the /Za flag (disable MS proprietary extensions to C++), then an error pops up complaining that the copy ctor is not accessible and thus, by the rules of C++, optimizing it away is not legal. This is called elided copy constructor, and there's a paragraph in the standard dealing with the issue. Also Stroustrup in The C++ Programming Language, Special Edition states the sequence of calls for an expression of a form analogous to this in an example. If you're interested I could try to locate the exact paragraph for you.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
kilowatt wrote:
By calling:
string s1 = "the truth is out there";
you are first invoking the default constructor
That is incorrect. The following two lines are exactly the same:
string s1 = "the truth is out there";
string s1 ("the truth is out there"); They both call the one-parameter constructor.
--Mike--
"There are three things I can never remember, 1: um... 2: um... 3: um..."
-- Victor Borge
My really out-of-date homepage
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan and Jamie Salé.
|
|
|
|
|
Not exactly. If you like C++ standard subtleties, please take a look at my post above.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
What message should my app handle when user shutdown/logoff the system?
thanx
|
|
|
|
|
The system will send a WM_QUERYENDSESSION message to all of the applications asking if it is OK to shutdown windows, if any one of them returns false, then windwos will quit shutting down.
After the system sends the WM_QUERYENDSESSION message, your app will receive a WM_ENDSESSION message indicating whether or not windows is actually going to shut down and your app should shut down.
|
|
|
|
|
Thanx ,
Where should i handle that ?
|
|
|
|
|
Handle it in your main window , WindowProc function, this is the window that it gets sent to.
|
|
|
|
|
|
Does anyone know if it is possible to login to a winNT box from over a network. I.E the other computer is at its login screen therefore standard screen sharing programs cannot be used as I think they require a server to be running on the unlogged in machine.
Thanks
Coremn
---------
I sig there for I am
(not an animal!!!)
|
|
|
|
|
Windows NT terminal server will do this, as will "VNC", provided you "install vnc as a service"
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
I am trying to create an example using for_each to delete objects where they are stored in a container as pointers. I have this:
class pType
{
public:
pType::pType()
{
cout << "Constructor\n";
}
pType::~pType()
{
cout << "Destructor\n";
}
};
template <class T> void ptrErase(T * type)
{
delete type;
};
in main()
vector <pType *> vecpType;
for (i = 0;i < 20; ++i)
{
pType * pInt = new pType;
vecpType.push_back(pInt);
}
for_each(vecpType.begin(), vecpType.end(), ptrErase<pType>);
I get a linker error,
STL.obj : error LNK2001: unresolved external symbol "void __cdecl ptrErase(class pType *)" (?ptrErase@@YAXPAVpType@@@Z)
Debug/STL.exe : fatal error LNK1120: 1 unresolved externals
I am not as familiar with templated functions as I would like, could someone tell me what I am doing wrong pls ?
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
|
|
|
|
|
Change this:
template<class T>
void ptrErase(T * type)
{
delete type;
};
to this:
template<class T>
struct ptrErase : public unary_function<T, void>
{
void operator() (T type)
{
delete type;
}
};
because the for_each function requires a function object rather than a simple function.
The operator() allows you to call the name of your object, just like it were a function call.
|
|
|
|
|
Thanks - I had contemplated that this might be the problem after posting, but I was hoping not for ease of readability ( it's a beginners tutorial after all ).
This works great - thanks again.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
|
|
|
|
|
Some of the features in STL, and C++ for that matter, are convoluted, and I think that is what shys alot of people away from these features in the first place. This is a shame because learning some of these techniques can lead to very elegant solutions.
|
|
|
|
|
In case you wonder, your original code should have worked, but it doesn't due to a limitation of VC++ --it lacks support for so called explicit instantiation of template functions, or better stated, its support is broken. Don't know the status of this bug in VC++ 7.0.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Have a little question for you all. In regards to STL -- what distribution of it do you use? (assuming that you use it of course).
Visual C++ 6 has STL support (to a point), it seems incomplete though. I downloaded and started using the SGI STL which is complete (except I found that it only supports the depricated <iostream.h> not the new <iostream> .. what the heck is going on?
I'm a little confused .. maybe I'm just not seeing some obvious small detail here ..
Travis D. Mathison ---
--- After three days without programming, life becomes meaningless ...
|
|
|
|
|
I prefer the SGI STL implementation.
A while back I created a test program to compare STL versus MFC's container classes, and MFC's classes kicked butt over STL, that is the STL that is included with DevStudio. Then I recompiled the program with SGI's version and the competition was feirce depending on the way that the container was used. Sometimes STL was more efficient, others it was MFC container classes, which is to be expected.
The SGI implementation supports other classes that I don't think are in the standard yet. One of my favorite classes in the SGI implementation is the hashmap and hashset, this is used in the exact same way that the map and set are used, but the underlying representation is a hash table rather than a table.
I could not find what you were talking about between depricated and new, could you give more details on that?
|
|
|
|
|
What I meant was that (this is only one instance), the SGI STL has been designed to support the old header file naming style. That is, you would include iostream.h <-- rather than iostream -- if you do include iostream so you can access the new features you will get a lot of errors because it doesn't handle it yet for some reason.
Travis D. Mathison ---
--- After three days without programming, life becomes meaningless ...
|
|
|
|