Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > auto_ptr problem in visual c++
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 18th November 17:14
mark s. ogilvie
External User
 
Posts: 1
Default auto_ptr problem in visual c++



I'm just starting with "The C++ standard Library, a tutorial and reference
by Nicolai Josuttis(10th printing) and that auto_ptr example is not working
properly. When I run this:

#include <iostream>
#include <memory>
using namespace std;

/* define output operator for auto_ptr
* - print object value or NULL
*/
template <class T>
ostream& operator<< (ostream& strm, const auto_ptr<T>& p)
{
// does p own an object ?
if (p.get() == NULL) {
strm << "NULL"; // NO: print NULL
}
else {
strm << *p; // YES: print the object
}
return strm;
}

int main()
{
auto_ptr<int> p(new int(42));
auto_ptr<int> q;

cout << "after initialization:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;

q = p;
cout << "after assigning auto pointers:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;

*q += 13; // change value of the object q owns
p = q;
cout << "after change and reassignment:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
}

Which is the example in the book, I get

after initialization:
p: 42
q: NULL
after assigning auto pointers:
p: 42
q: 42
after change and reassignment:
p: 55
q: 55
Press any key to continue

Not what I should get according to both the book and my understanding of
auto-pointers. I've check the Josuttis home page and it is not listed under
erata or compilier quirks. I am using Microsoft visual c++ version 6.0.
Could the problem be somewhere in the build settings? Or does visuall c+
simply handle auto_ptr like this?

Mark
  Reply With Quote


 


2 18th November 17:14
andré_pönitz
External User
 
Posts: 1
Default auto_ptr problem in visual c++



The result should be

after initialization:
p: 42
q: NULL
after assigning auto pointers:
p: NULL
q: 42
after change and reassignment:
p: 55
q: NULL


That might be the problem.

MSVC++ 6.0 hardly qualifies as C++ compiler when it comes to Standard
C++.

g++ gets it right btw.

Andre'
  Reply With Quote
3 18th November 17:14
guy harrison
External User
 
Posts: 1
Default auto_ptr problem in visual c++


^^^
Bad. It should have handed over ownership here.

It may be but you'd be best off checking on a VC group. This can't have gone unoticed.


If it does, then its auto_ptr may as well not exist. Take a look in the FAQ.
It never hurts to have more than one compiler.


--
Guy Harrison
  Reply With Quote
4 18th November 17:14
mark s. ogilvie
External User
 
Posts: 1
Default auto_ptr problem in visual c++


I've been thinking about getting another compiler for non windows
programing. This is a good reason.

Thanks

Mark
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666