Re: compile error about auto_ptr

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:878AA930-A216-4BE1-8B32-BDACF5E6EFB9@xxxxxxxxxxxxx
`auto_ptr' may be copied and see "vc.push_back (pi);" statement.
The signature of `vector::push_back' method requires const
parameter. But `auto_ptr' doesn't provide such constructor. That's

Sorry, Alex. I do not agree that the error comes from the push_back
method input. There is no problem to pass non-const reference to
const reference, but not vice versa (this is the root cause of this
issue).

To prove it, here is my code. Any comment?

class Foo
{
};

class myVector{
public:
void push_back (const Foo& input)
{
}
void foo()
{
Foo f;
push_back (f);
}
};

To make this resemble the situation with vector and auto_ptr, do two
things.

a) Add a Foo copy-constructor that takes Foo& (thus suppressing an
implicitly defined one that takes const Foo&):

class Foo {
public:
Foo(Foo&) {}
};

b) In push_back, actually make a copy of the parameter, as
vector::push_back has to do:

void push_back (const Foo& input) {
Foo copy = input;
}

See if you get an error now.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: Funky function
    ... >> It makes it possible to call this member function on a const object ... >> object may or may not be const). ... int Data; ... in the next line you try to call a function foo on MyData. ...
    (comp.lang.cpp)
  • Re: is const function faster than non-const?
    ... > void foo() const; ... > void goo(); ... what does compiler optomization does to const function? ...
    (comp.lang.cpp)
  • Re: Works in Visual C++ 2003 but not in 2005
    ... void action(const Foo* const & cptr); ...
    (microsoft.public.dotnet.languages.vc)
  • Inheritance and object factories...HELP!!
    ... Now foois a const member function so the way it has to work is to ... The member function foo creates a new object on the stack and returns it ... The foooperation is identical for deriv1 and deriv2 except that it ...
    (comp.lang.cpp)
  • Re: compile error about auto_ptr
    ... I have tried and it gives the same error message. ... The signature of `vector::push_back' method requires const ... void push_back (const Foo& input) ...
    (microsoft.public.vc.language)