Here is the problem i am facing, does anyone have solution?
Class A: public class B
{
// I want to pass a reference of B to Function
}
void ClassC::Function(class& B)
{
//do stuff
}
The way you are declaring the class is wrong:
class A : public B // no more class keyword here
{
}; // note the semicolon
void ClassC::Function(const B &b) // this is how you declare a parameter of type B&
{
}
You simply need to pass the object of type A to the Function. It'll work.
It's good to declare the parameter as const if you want to take derived types too.
To pass the this instance, you'd simply call:
classCObject.Function(*this);
Are you just having trouble with the syntax? It should be
void ClassC::Function(B& b)
{
b.DoSomething();
}
to make b a reference of type B.
Related
My problem is about passing a member function from a Class A, to a member function of a Class B:
I tried something like this :
typedef void (moteurGraphique::* f)(Sprite);
f draw =&moteurGraphique::drawSprite;
defaultScene.boucle(draw);
moteurGraphique is A class, moteurGraphique::drawSprite is A member function,
defaultScene is an instance of B class, and boucle is B member function.
All that is called in a member function of A:
void moteurGraphique::drawMyThings()
I tried different ways to do it, that one seems the more logical to me, but it won't work!
I got:
Run-Time Check Failure #3 - The variable 'f' is being used without being initialized.
I think I am doing something wrong, can someone explain my mistake ?
C++11 way:
using Function = std::function<void (Sprite)>;
void B::boucle(Function func);
...
A a;
B b;
b.boucle(std::bind(&A::drawSprite, &a, std::placeholders::_1));
Member functions need to be called on objects, so passing the function pointer alone is not enough, you also need the object to call that pointer on. You can either store that object in the class that is going to call the function, create it right before calling the function, or pass it along with the function pointer.
class Foo
{
public:
void foo()
{
std::cout << "foo" << std::endl;
}
};
class Bar
{
public:
void bar(Foo * obj, void(Foo::*func)(void))
{
(obj->*func)();
}
};
int main()
{
Foo f;
Bar b;
b.bar(&f, &Foo::foo);//output: foo
}
Can't you make drawMyThing a static function if you don't need to instantiate A, and then do something like :
defaultScene.boucle(A.drawMyThing(mySpriteThing));
?
I am looking for a way to save a pointer to a member function of a derived class.
For exemple:
class A
{
public:
typedef void (A::*FunctionP) (int a);
};
class B : A
{
public:
void Test(int a)
{
//Do stuff
}
B()
{
FunctionP theFunc = &B::Test;
}
};
The following code dose not compile..
Is there another way to do this? (Using template's maybe, or boost)
(Btw class A is an abstract class witch one of its implementations is invoking functions saved as FunctionP)
Thank you
Just use a static_cast:
FunctionP theFunc = static_cast<FunctionP>(&B::Test);
Coliru: http://coliru.stacked-crooked.com/a/2cfed4926aed43db
Still, it might be even better to use std::function and std::bind, depending on your needs.
I want to create a member function with the same name of the returning type. Example:
class A { };
class B {
public:
A& A() { return *a; }
private:
A* a;
};
However, the compiler won't let me. I tried to change the type of the member return type to ::A (as sugested here, but with no avail. I know I could just change the member name, but I just want to understand why does it has this restriction, and what are my workarounds.
If you declare a member called A you can no longer use the type A without an explicit namespace. You need to change every occurrence of the type A to ::A.
The corrected code looks like:
class A { };
class B {
public:
::A& A() { return *a; }
private:
::A* a;
};
Fixed code on codepad:
http://codepad.org/cilF9rKm
That's because a member with the same name as the class is a constructor. However, you try to declare one with a type, which is an error. You can only define constructors the way the language wants you too (without an explicit return type).
For example, if you had a method in B that said
A x = A();
it is ambiguous whether you are calling B::A() or are constructing a new A object.
Here are the requirements posed by my application. I have a class A, that accepts a function pointer say cFunc, Basically in my implementation of A, I have it call cFunc multiple times.
The cFunc pointer itself should point to different functions depending upon the application. Thus for each application I create a class with the same function definition as cFunc, however I cannot assign the class's member function to this pointer
class A {
typedef double (*Def_CFunc)(std::vector<double>);
A(Def_CFunc _cFunc) { // Some implementation}
// Other Functions
};
class B { double someFunc(std::vector<double> b); };
class C { double someOtherFunc(std::vector<double> a); };
int main () {
B firstObj;
C secondObj;
// Depending upon the situation, I want to select class B or C
double (*funcPointer)(std::vector<double>) = firstObj.someFunc; // Error in this line of code
A finalObj(funcPointer);
}
So how do I make it such that any class with a member function of the given format can be used to initialize the class A?
I'm not sure what exactly your requirements are, but it looks like you want an interface (or abstract base class in C++ lingo).
If both B and C inherit from a common base class, you can pass a pointer to this base class and invoke functions on it:
class I { virtual double func(std::vector<double> a) = 0; }
class B : public I { double func(std::vector<double> a); };
class C : public I { double func(std::vector<double> a); };
You can pass an I* pointer to A and just use i->func.
Pointer to member function has different syntax than pointer to ordinary function and can only point to a method of one given class. To be able to point to methods in different classes use boost::function or if C++11 is available use std::function. These can hold any method or function of a given signature.
What you need is std::function together with either std::bind or lambda expressions (or the Boost equivalent of the first two), because member function pointers don't allow you to do that.
You can do it using std::bind + std::function. Lets write some template class wrapper, that takes any static type as input. Then use this wrapper in free function switch_obj. Usage is very simple.
typedef std::function<double(std::vector<double>)> my_foo;
template<class C>
struct switcher
{
static my_foo convert(C* obj)
{
return my_foo( std::bind(&C::someFunc,obj,std::placeholders::_1) );
}
};
template<class T>
my_foo switch_obj(T* obj)
{
return switcher<T>::convert(obj);
}
void main()
{
B firstObj;
C secondObj;
auto f = switch_obj(&firstObj);
A a(f);
}
I have the following situation:
class B
{
public:
void methodInB();
};
class C
{
public:
void methodInC();
};
class A
{
public:
void methodInA();
private:
B objB;
C objC;
};
void A::methodInA()
{
objB.methodInB();
}
int main()
{
A objA;
objA.methodInA();
return 0;
}
I want to be able to call C::methodInC() from within B::methodInB(), but I'm not sure what the way to go about it would be (not without messing with globals).
My first idea was to add a C* pC pointer as a member of B, and then from methodInB() call it as pC->methodInC. This would require I set the pointer from within A before using the method (possibly in A's constructor). My problem is I may need to call other objects from within B if I add a D and E objects, and I don't want to fill the class definition with pointers.
Is there some other way of doing this? An implicit reference to the object the object belongs to? Kind of like this but for the parent? So I could at least do parent->objC->methodInC()?
I think the cleanest way would be to "inject the dependency", that is, to pass objC to methodInB, which would then invoke methodInC on that object:
void A::methodInA()
{
objB.methodInB(objC);
}
// ...
void B::methodInB(C &objC)
{
objC.methodInC();
}
Let every class B, C, D, E, etc. have a pointer to the A object of which they are subobjects.
class A;
class C;
class B
{
A* pA;
void MethodB();
};
...
void B::MethodB
{
(pa->CObject).MethodC();
}