Re: Subclassing CgiInput

[email protected] Fri, 20 Apr 2007 15:40:10 +0200
Newsgroups gmane.comp.gcc.cgicc.general
Message-ID <[email protected]>
Thanks for the help!

> On 2007-04-19T20:49:30+0200, [email protected] wrote:
> > FastCgiInput *   i = new FastCgiInput();
> > CgiEnvironment * e = new CgiEnvironment(i);
> > 
> > A call to i->getenv("QUERY_STRING") is successful and returns the right
> query string, but a call to e->getQueryString() returns an empty string.
> > 
> > The documentation says, that the operator= must be overloaded. What
> > exactly does that mean? What signature should I use when overloading
> > operator=? How does this affect the behaviour of the code?
> 
> It means you have to define:
> 
> FastCgiInput & 	operator= (const FastCgiInput &)
> 
> The default copy constructor is currently called instead.  If you have
> not, just trace the original implementation and do the same with yours
> and see what happens.
> 
> 
> /Allan

I resolved the problem: As you mentioned above, the default copy constructor is called, if no operator= is present. I tried to implement one, but that didn't change the behaviour. Then I took again a closer look at my FastCgiInput class an saw the problem: I "overloaded" the functions

                virtual
                size_t
                read(char * Data, size_t aLength) const;

                virtual
                std::string
                getenv(const char * aName) const;

as described in my first post. But this is not an overloading, because I made this tow functions "const". The right signature to use when overloading this functions must be

                virtual
                size_t
                read(char * Data, size_t aLength);

                virtual
                std::string
                getenv(const char * aName);

With this change it works fine. In the current implementation both, the default constructor and the default copy-constructor, are protected and only callable from friend classes. Additionaly I removed the operator= from the implementation and it still works fine. I wondered about the comment in the documentation which says, that operator= must be overloaded, because the source code of my cgicc library shows, that all CgiInput objects are managed by pointers. So there is never a call to operator= or to the default copy constructor during the contruction of Cgicc or CgiEnvironment objects. At least this implementation works now. Thanks to Allan for the help! I hope this post can help anyone to avoid this "trap" when "overloading" functions, because the compiler cannot guess, that this is not what I intended it to be.

The only question which is left is: What does the comment "operator= must be overloaded" mean in the documentation? Is it a comment from an older version?

Thanks,
Matthias