Reputation: 597
I am using an STL vector that is a vector of Parameters.
std::vector<Parameter> foo;
I was trying to find a way to add Parameter objects to the vector without doing this:
Parameter a;
foo.push_back(a);
I came across an implementation that did this:
foo.push_back(Parameter()); //Using the Parameter constructor
I thought that when I created an object the constructor is called not vise versa. Why can I pass a constructor to a function?
Upvotes: 7
Views: 11222
Reputation: 1369
This subject is old but I'd like to add something, if you truly want to pass a constructor as parameter (as function pointer) without creating a temporary object you can do this hack if you're using a recent enough C++ standard
foo([](){ return MyClass(); });
Upvotes: 0
Reputation: 1154
The other point we're missing here is that to pass a function as a parameter you do not use parentheses at all. Here's an example of passing a function as a parameter:
#include <algorithm>
#include <vector>
#include <iostream>
// initialise sum and summing function
int sum = 0;
void sum_numbers( const int& number );
// Create a test for our sum function
int main(int argc, const char * argv[])
{
// create some numbers
std::vector<int> numbers;
numbers.push_back( 34 );
numbers.push_back( 73 );
numbers.push_back( 14 );
// passing "sum_numbers" function as a parameter
std::for_each( numbers.begin(), numbers.end(), sum_numbers );
// check that our sum function worked
std::cout << "The sum of numbers is: " << sum << std::endl;
return 0;
}
// add the current number to the sum
void sum_numbers( const int& number )
{
sum += number;
}
Upvotes: 0
Reputation: 32894
foo.push_back(Parameter());
is passing a temporarily constructed Parameter object to push_back
and not the constructor i.e. Parameter()
is a call to create an object of type Parameter
on the stack and pass it to the push_back
function of vector
, where it gets moved/copied. Thus what gets passed is not the constructor itself, but a constructed object only.
It's just a shorthand way of writing Parameter a; foo.push_back(a);
when one is sure that a
is not used anywhere down the line; instead of declaring a dummy, temporary variable, an anonymous temporary is created and passed.
These might be useful if you want to learn more about temporaries:
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=198
http://msdn.microsoft.com/en-us/library/a8kfxa78%28v=vs.80%29.aspx
Upvotes: 8
Reputation: 1884
when you call Parameter()
a temporary object is created and passed to foo.push_back()
function. Previously you declared the object with name Parameter a;
and passed it to like this foo.push_back(a)
. By doing like this you can use the object named a
down the line of your program.
Upvotes: 1
Reputation: 7136
You are not passing a constructor, but instead passing a temporary object. When you do Paramater()
because of the pharenthesis, it creates a object. Its kind of like a function call for example getInput()
.
Upvotes: 0
Reputation: 96800
That line will create a temporary instance of Parameter
and copy it into Foo
. Assuming this is pre-C++11 code. The new std::vector<T>::push_back
has an overload for rvalues in which case there will be no copies.
Upvotes: 4