Related
I remember vaguely that python allowed something like
def foo( x ):
....
f = foo( 5 )
Is something like that possible in c++ so that if I have a member function
class C {
void foo( int x ) { ... }
so that I can define a pointer or variable that would effectively point at foo( 5 )
The reason why I want to do this is because I have many listeners that I need to subscribe to a callback and keep information who gets called
class C {
map<int, ptrSender> m_sender;
void subscribe() {
for (const auto& p : m_sender) {
p .second->register( Callback( this, &C::onCall ) )
}
My problem is that the onCall does not return which sender called back, but I would need this information. So, instead of doing something like this
void subscribe() {
m_sender[0]->register( Callback( this, onCall_0 ) );
m_sender[1]->register( Callback( this, onCall_1 ) );
....
void onCall( int sender_id ) { ... }
void onCall_0() { onCall( 0 ); }
void onCall_1() { onCall( 1 ); }
....
I was hoping I could pass something into register that would return a call with a preset argument. Is this possible?
EDIT: I am trying to use a lambda function, but I am running into the following problems
auto setCall= [this]( int v ) { &C::onCall( v ); }
gives the compile error
lvalue required as unary&opeand
This
auto setCall= [this]( int v ) { C::onCall( v ); }
....
p.second->register( Callback( this, &setCall( p.first) ) ); /// <__ error now here
complains again, now in the second line
lvalue required as unary&operand
and this
auto setCall= [this]( int v ) { C::onCall( v ); }
....
p.second->register( Callback( this, setCall( p.first) ) ); /// <__ error now here
complains about invalid use of void expression, but I assume I have to pass in a reference to make the register function happy
Callback seems to be defined as
# define CallBack(obj,func) ProfiledBasicCallBack(obj,fastdelegate::FastDelegate0<void>(obj,func),#func)
Yes, you can use std::bind. Example usage: http://ideone.com/akoWbA.
void foo( int x ) { cout << x << endl; }
auto x = std::bind(foo, 5);
x();
However, with modern C++, you should use a lambda. Like so:
void foo( int x ) { cout << x << endl; }
auto x = []() { foo(5); };
x();
Note that this foo function is outside of the class C in this example. If you wish to contain it inside, then with std::bind you need to pass the instance of the object you wish to call on, e.g.
C c;
auto x = std::bind(&C::foo, &c, 5);
x();
or with lambdas:
C c;
auto x = [&c]() { c.foo(5); };
x();
What you are looking for is std::bind(). It takes one callable object, and gives you another callable object with predefined values for its parameter, and maybe some optional parameters forwarded to it.
A word of warning: this is a fairly steep learning curve. You need to understand templates.
If you want to bind a parameter value to a compile-time constant argument (like 5 in your example), then the problem can be solved by introducing a simple wrapper function that will call your function while passing the desired constant values as corresponding arguments.
But when the argument is a run-time value, then the answer is no: it is generally not possible to create a credible implementation of such function pointer binding in C++ (unless you are using some compiler-specific extension).
However, in C++ you have a variety of alternative tools at your disposal. You can create a function object that will mimic the functionality you desire. Such function object can be created by using std::bind, by using lambda-expressions, or even implemented manually.
The resultant function object will be "callable" and will behave similarly to function pointer at superficial level, but nevertheless it won't be a function pointer, won't be convertible to a function pointer and won't be accepted where a genuine function pointer is required. In other words, if your register method is declared to expect a function pointer as its second argument, then there's nothing you can do here. Neither std::bind, nor lambdas, nor anything else in the language will help you to achieve this kind of parameter binding.
For this reason it is generally a good idea to steer clear of function pointers in such designs and implement such functionality in terms of generic callable objects. The simplest thing to use might be std::function objects in place of raw function pointers.
The following test code demonstrates an issue that I am having in a much larger application. In the application I have a service that "provides" several servers that are all derived from one base class. I then use createInstance to get "access" to a specific server based on a server type ('n' used below). dynamic_cast is then used to cast as the appropriate server. This all works fine.
The problem is when I try to use the deleteInstance to go back to the service and delete it, cleaning up any internal server related data. I cannot seem to find a good passing mechanism or if it is even valid way of achieving what I am doing.
#include <iostream>
#include <string>
class MM
{
public:
virtual ~MM() {}
virtual void start() = 0;
};
class M1 : public MM
{
public:
void start()
{
std::cout << "M1 start" << std::endl;
}
};
class M2 : public MM
{
public:
void start()
{
std::cout << "M2 start" << std::endl;
}
void start( const std::string strName )
{
std::cout << "M2 start - " << strName << std::endl;
}
};
MM * createInstance( int n )
{
if( 2 == n )
{
return new M2;
}
else
{
return new M1;
}
}
void deleteInstance( MM * & pInstance )
{
delete pInstance;
pInstance = NULL;
}
void deleteInstance2( MM ** ppInstance )
{
delete *ppInstance;
*ppInstance = NULL;
}
int main( int argc, char *argv[] )
{
M1 *pM1 = dynamic_cast<M1 *>( createInstance( 1 ) );
M2 *pM2 = dynamic_cast<M2 *>( createInstance( 2 ) );
pM1->start();
pM2->start();
pM2->start( "test" );
deleteInstance( pM1 );
deleteInstance( pM2 );
//deleteInstance2( &pM1 );
//deleteInstance2( &pM2 );
return 0;
}
To complete the info, the error that I am receiving for deleteInstance implementation:
68:25: error: invalid initialization of reference of type ‘MM*&’ from expression of type ‘M1*’
46:6: error: in passing argument 1 of ‘void deleteInstance(MM*&)’
69:25: error: invalid initialization of reference of type ‘MM*&’ from expression of type ‘M2*’
46:6: error: in passing argument 1 of ‘void deleteInstance(MM*&)’
and for deleteInstance2:
70:27: error: invalid conversion from ‘M1**’ to ‘MM**’
70:27: error: initializing argument 1 of ‘void deleteInstance2(MM**)’
71:27: error: invalid conversion from ‘M2**’ to ‘MM**’
71:27: error: initializing argument 1 of ‘void deleteInstance2(MM**)’
The problem is that binding a pointer to the derived type with a reference to a pointer to the base type would break the type system. Consider this motivating example:
void resetPtr( base*& b ) {
static base instance;
b = &instance;
}
int main() {
derived *d;
resetPtr( d ); // Now d points to a base, not a derived object!!!!
}
While you can work around this as some other answer points out (for example through the use of templates that will infer the appropriate type and so on), I would recommend that you redesign and pass the pointer by value.
Why is it a bad idea to reset the pointer to NULL after deletion?
The problem with reseting the pointer to NULL is that it does not really solve any problem, and adds problems of its own.
It does not solve the problem of knowing whether pointers are valid in your application, as in the general case you can have more than one pointer to a given object, and because you only delete one of them, only one of the pointers will be reset to NULL, and you are left (at least in most cases) with the same situation you had in the beginning.
It can help hide bugs in the logic of your application: after you reset the pointer to NULL, any potential issue in your application by which you delete the pointer twice will be hidden, as it is safe to delete a NULL pointer. While you might think that this is a good idea --after all, it avoids crashing your application-- in the long term it is a bad idea, since the core issue is still there: the design fails to provide proper ownership semantics.
The problem has nothing to do with base class vs. derived class pointers; the problem is simply that you've declared your method to accept a pointer to pointer to MM as an argument, and you're passing just a pointer to MM.
You could pass a pointer to MM by reference -- i.e.,
void deleteInstance( T* &pInstance ) ...
I'm not sure I like what you're trying to do, but I can't put my finger on why - I think the idea is fine. .. but heres a way you could implement it .
template<typename T>
void deleteInstance( T * & pInstance )
{
// This conversion is here so you get a nice error if
// you try to use it on a type that isn't derived from MM.
MM* tmp = pInstance;
delete tmp;
pInstance = NULL;
}
The reason why deleteInstance is not working is that you are taking a non-const reference to a temporary created by the conversion of M1* or M2* to MM*.
The reason why deleteInstance2 is not working is that Derived** is not convertible to Base**. This faq explains it very well.
How do we handle more than one output parameters in C++.I am beginner in C++ and currently i am trying to write a function A which calls another function B of some other class,Function B consists of 6 parameters in total ,of which three are input parameters and the rest three are output parameters.How can i access all the three output parameters within my function A?I tried to do it in the following way...Can anyone help me to correct my code if i have gone wrong..?Please do help me friends..
class A ::functionA()
{
int in_a=1;
string in_b= "name";
int in_c=3;
int ot_a=0;
int ot_b=0;
string ot_s1=""
ClassB *classB();
classB = classB.functionB(in_a,in_b,in_c,ot_a,ot_b,ot_s1); //is this way correct?
ot_a= ? ;
ot_b=? ;
ot_s1=?
}
can i use something like ot_a=classB.ot_a ?Please help me...
You have got the basic syntax of C++ wrong. ClassB *classB(); does not create any object, it declares a function prototype of function classB which returns ClassB*. To create a object you should do ClassB b; and then use b as you have done. The output variables will be correctly filled up by the function if it is taking its parameter by reference.
For multiple return values, you got generally two choices:
return a struct containing your return values
pass the return values in per reference.
Both examples demonstrated:
// first approach, struct return
struct myReturns{
int int_return;
float float_return;
};
myReturns MyFunc(int param1, char* param2, ...){
// do some stuff with the parameters
myReturns ret;
ret.int_return = 42;
ret.float_return = 13.37f;
return ret;
}
// calling it:
myReturns ret = MyFunc(/*pass your parameters here*/);
int i = ret.int_return;
float f = ret.float_return;
// second approach, out parameters
void MyFunc(int inParam1, char* inParam2, int& outInt, float& outFloat){
// do some stuff with the parameters
outInt = 42;
outFloat = 13.37f;
}
// calling it:
int i;
float f;
MyFunc(/*your parameters here*/,i,f);
// i and f are now changed with the return values
As mentionned in Xeo's answer, you can use return structures or references.
There is another possibility, to use pointers.
Pointers allows you to do one thing : if the function you call can be used to compute multiple informations, but you don't want all of them, you can pass NULL as the value of the pointer so that the function knows it doesn't need to fill these informations.
Of course, the function you call needs to be designed that way, it's not automatic.
void f()
{
type1* p1 = new type1();
type2* p2 = NULL
g(p1, p2);
}
void g(type1* param1, type2* param2)
{
//Do some computation here
if (param1 != NULL)
{
//Do something here to fill param1
}
if (param2 != NULL)
{
//Do something here to fill param2
}
}
But as a general rule, it's better to use references when you can, and pointers when tou have to. If the function doesn't handle the case when a pointer passed to it is NULL, you will end with a crash. References can't be NULL, so they avoid this problem.
Answer is: references.
ClassB *classB();
classB = classB.functionB(in_a,in_b,in_c,ot_a,ot_b,ot_s1);
By looking . operator after classB, I assume that you are thinking classB is an object. No, it is not.
ClassB *classB();
The above statement says - classB() is a function that takes no parameters and return type is a reference to ClassB.
If you can change functionB() then use pointers as parameters. This way you can change the value inside functionB() and they will be changed directly in functionA().
Suppose I have a class:
class test {
public:
void print();
private:
int x;
};
void test::print()
{
cout<< this->x;
}
and I have these variable definitions:
test object1;
test object2;
When I call object1.print() this happens to store address of object1 and so I get x from object1 printed and when I call object2.print() this happens to store address of object2 and I get x from object2 printed. How does it happen?
Each non-static member function has an implicit hidden "current object" parameter that is exposed to you as this pointer.
So you can think that for
test::print();
there's some
test_print( test* this );
global function and so when you write
objectX.print();
in your code the compiler inserts a call to
test_print(&objectX);
and this way the member function knows the address of "the current" object.
You can think of the this pointer being an implicit argument to the functions. Imagine a little class like
class C {
public:
C( int x ) : m_x( x ) { }
void increment( int value ) {
m_x += value; // same as 'this->m_x += value'
}
int multiply( int times ) const {
return m_x * times; // same as 'return this->m_x * times;'
}
private:
int m_x;
};
which allows you to write code like
C two( 2 );
two.increment( 2 );
int result = two.multiply( 3 );
Now, what's actually happening is that the member functions increment and multiply are called with an extra pointer argument, pointing to the object on which the function is invoked. This pointer is known as this inside the method. The type of the this pointer is different, depending on whether the method is const (as multiply is) or not (as is the case with increment).
You can do something like it yourself as well, consider:
class C {
public:
C( int x ) : m_x( x ) { }
void increment( C * const that, int value ) {
that->m_x += value;
}
int multiply( C const * const that, int times ) const {
return that->m_x * times;
}
private:
int m_x;
};
you could write code like
C two( 2 );
two.increment( &two, 2 );
int result = two.multiply( &two, 3 );
Notice that the type of the this pointer is C const * const for the multiply function, so both the pointer itself is const but also the object being pointed to! This is why you cannot change member variables inside a const method - the this pointer has a type which forbids it. This could be resolved using the mutable keyword (I don't want to get side-tracked too far, so I'll rather not explain how that works) but even using a const_cast:
int C::multiply( int times ) const {
C * const that = const_cast<C * const>( this );
that->m_x = 0; // evil! Can modify member variable because const'ness was casted away
// ..
}
I'm mentioning this since it demonstrates that this isn't as special a pointer as it may seem, and this particular hack is often a better solution than making a member variable mutable since this hack is local to one function whereas mutable makes the variable mutable for all const methods of the class.
The way to think about it is that this is simply a pointer to the memory for whichever object you're currently working with. So if you do obj1.print(), then this = &obj1;. If you do obj2.print(), then this = &obj2;.
this has different values for different objects
Each instance of class test gets it's own copy of member variable x. Since x is unique for each instance, the value can be anything you want it to be.
The variable this, refers to the instance to which it is associated. You don't have to use the variable 'this'. You could just write:
void test::print()
{
cout << x;
}
Is it possible to declare a variable in c++ without instantiating it? I want to do something like this:
Animal a;
if( happyDay() )
a( "puppies" ); //constructor call
else
a( "toads" );
Basially, I just want to declare a outside of the conditional so it gets the right scope.
Is there any way to do this without using pointers and allocating a on the heap? Maybe something clever with references?
You can't use references here, since as soon as you'd get out of the scope, the reference would point to a object that would be deleted.
Really, you have two choices here:
1- Go with pointers:
Animal* a;
if( happyDay() )
a = new Animal( "puppies" ); //constructor call
else
a = new Animal( "toads" );
// ...
delete a;
or with a smart pointer
#include <memory>
std::unique_ptr<Animal> a;
if( happyDay() )
a = std::make_unique<Animal>( "puppies" );
else
a = std::make_unique<Animal>( "toads" );
2- Add an Init method to Animal:
class Animal
{
public:
Animal(){}
void Init( const std::string& type )
{
m_type = type;
}
private:
std:string m_type;
};
Animal a;
if( happyDay() )
a.Init( "puppies" );
else
a.Init( "toads" );
I'd personally go with option 2.
You can't declare a variable without calling a constructor. However, in your example you could do the following:
Animal a(happyDay() ? "puppies" : "toads");
You can't do this directly in C++ since the object is constructed when you define it with the default constructor.
You could, however, run a parameterized constructor to begin with:
Animal a(getAppropriateString());
Or you could actually use something like the ?: operator to determine the correct string.
(Update: #Greg gave the syntax for this. See that answer)
I prefer Greg's answer, but you could also do this:
char *AnimalType;
if( happyDay() )
AnimalType = "puppies";
else
AnimalType = "toads";
Animal a(AnimalType);
I suggest this because I've worked places where the conditional operator was forbidden. (Sigh!) Also, this can be expanded beyond two alternatives very easily.
If you want to avoid garbage collection - you could use a smart pointer.
auto_ptr<Animal> p_a;
if ( happyDay() )
p_a.reset(new Animal( "puppies" ) );
else
p_a.reset(new Animal( "toads" ) );
// do stuff with p_a-> whatever. When p_a goes out of scope, it's deleted.
If you still want to use the . syntax instead of ->, you can do this after the code above:
Animal& a = *p_a;
// do stuff with a. whatever
In addition to Greg Hewgill's answer, there are a few other options:
Lift out the main body of the code into a function:
void body(Animal & a) {
...
}
if( happyDay() ) {
Animal a("puppies");
body( a );
} else {
Animal a("toad");
body( a );
}
(Ab)Use placement new:
struct AnimalDtor {
void *m_a;
AnimalDtor(void *a) : m_a(a) {}
~AnimalDtor() { static_cast<Animal*>(m_a)->~Animal(); }
};
char animal_buf[sizeof(Animal)]; // still stack allocated
if( happyDay() )
new (animal_buf) Animal("puppies");
else
new (animal_buf) Animal("toad");
AnimalDtor dtor(animal_buf); // make sure the dtor still gets called
Animal & a(*static_cast<Animal*>(static_cast<void*>(animal_buf));
... // carry on
Since c++17, there is now an overhead-free way to do this: std::optional. The code in this case would be:
#include <optional>
std::optional<Animal> a;
if (happyDay()) {
a.emplace("puppies");
} else {
a.emplace("toads");
}
The best work around is to use pointer.
Animal a*;
if( happyDay() )
a = new Animal( "puppies" ); //constructor call
else
a = new Animal( "toads" );
You can also use std::move:
class Ball {
private:
// This is initialized, but not as needed
sf::Sprite ball;
public:
Ball() {
texture.loadFromFile("ball.png");
// This is a local object, not the same as the class member.
sf::Sprite ball2(texture);
// move it
this->ball=std::move(ball2);
}
...
There is a way to do this without pointers/heap memory, this syntax is just a bit gibberish. Here is an example using std::string. I don't recommend doing this unless you really need the performance.
uint8_t object[sizeof(std::string)];
int main() {
if(true)
new(&object) std::string("Your arguments");
else
new(&object) std::string("Your other arguments");
(*(std::string*)(&object)).append("");
std::cout << (*(std::string*)(&object));
return 0;
}
The annoying part about this is you have to cast object to a string every time you want to use it:
(*(std::string*)(&object))
Yes, you can do do the following:
Animal a;
if( happyDay() )
a = Animal( "puppies" );
else
a = Animal( "toads" );
That will call the constructors properly.
EDIT: Forgot one thing...
When declaring a, you'll have to call a constructor still, whether it be a constructor that does nothing, or still initializes the values to whatever. This method therefore creates two objects, one at initialization and the one inside the if statement.
A better way would be to create an init() function of the class, such as:
Animal a;
if( happyDay() )
a.init( "puppies" );
else
a.init( "toads" );
This way would be more efficient.