Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > parameter passing for virtual function

Reply
Thread Tools

parameter passing for virtual function

 
 
Ethan
Guest
Posts: n/a
 
      06-01-2009
class Base {
public:
virtual void foo (std::string& s) { // do crap here; }
};

class Derived : public Base {
// other crap
};

int main() {
Derived d;
std::string name("anyone");

d.foo (name); // ok

d.foo ("another one"); // error
d.foo (string("another one")); // error
}



g++ 3.4.6

when call the virtual function with temporary variable as parameters,
it always says:

not matching function for call too 'Derived::foo(std::string)'
candidates are: virtual void Base::foo(std::string&)

is it because no reference created for temporary variable? (in the 2nd
and 3rd cases);
I'd appreciate if someone can give a detailed explanation on this.
thanks!
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      06-01-2009
On 1 Jun., 17:18, Ethan <(E-Mail Removed)> wrote:
> * *virtual void foo (std::string& s) { // do crap here; }


[...]

> * *d.foo (name); * // ok
> * *d.foo ("another one"); // error
> * *d.foo (string("another one")); // error


[...]

> when call the virtual function with temporary variable as parameters,
> it always says:
>
> not matching function for call too 'Derived::foo(std::string)'
> candidates are: virtual void Base::foo(std::string&)


This has nothing to do with the function being virtual.

> is it because no reference created for temporary variable?
> (in the 2nd and 3rd cases); I'd appreciate if someone can
> give a detailed explanation on this.
> thanks!


The C++ standard doesn't allow a reference to non-const bind to a
temporary object. This decision is usually explained with an example
like this:

void foo(long & x) {
x = 42;
}

int main() {
int i = 23;
foo(i);
cout << i;
}

The code above doesn't actually compile due to the "reference to non-
const can't bind to rvalues"-rule. But it would compile without it and
probably surprize the author when he/she checkts the program's result.
It would print 23 and not 42 because prior reference binding 'i' is
converted to a long which results in a temporary. This temporary is
modified inside foo and destructed afterwards. What you /can/ do is
something like this:

void say(std::string const& x) {
cout << x;
}

int main() {
say("hello World!"); // creates temporary string
}

A reference-to-const /can/ bind to temporaries. This really ought to
be in the C++ FAQ [1]. But I couldn't find it. At least it should be
covered by your C++ textbook.

Cheers!
SG

[1] C++ FAQ Lite http://www.parashift.com/c++-faq-lite/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments