'pass through member function'

Started by
9 comments, last by cozzie 9 years, 9 months ago

Hi,

I was wondering if there's s a clean way to do this:

- assume we have class A and B

- class B has a member 'SetThingie(const int pValue)'

- class A has a vector of class B

- class A has a 'current B' int

- class A has a member 'SetThingie(const int pValue)', same as class B

- when class A SetThingie is called, it should call SetThingie of the class B object in the vector, with index 'current B'

Now the most straigt forward you would be something like this:


class ClassA
{
public:
    void SetThingie(const int pValue);

private:
    int currentIndex;
    std::vector<ClassB> mObjects;
}

class ClassB
{
public:
    void SetThingie(const int pValue);
}

void ClassA::SetThingie(const int pValue)
{
    mObjects[currentIndex].SetIndex(pValue);
}

Do you think there are easier/ cleaner ways to do this?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

Could you elaborate on what these names represent, what exactly is the role of a ClassA, ClassB and a Thingie?

Is this an implementation of the http://en.wikipedia.org/wiki/Flyweight_pattern ?

openwar - the real-time tactical war-game platform

Hi.

Class A is the ShaderManager class, class B is a individual effect.

I want to be able to set shader constants through the ShaderManager.

In pseudo code:

ShaderManager->SetCurrentPermutation(5);

ShaderManager->SetWorldMatrix(myMatrix);

The SetWorldMatrix should call the SetWorldMatrix of the effect class,

ShaderManager class has a std::vector<EffectClass>

I don't think it's a flyweight pattern, because the member functions are not 100% the same (the ShaderManager class functions simply call the similar named function of the Effect class (for the 'current effect' = index of the vector of effect objects)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Seems reasonable to me. Is there something about this approach that you're unhappy with? One can over-think things like this.

You could treat Class A as an iterator:

class B;
class A {
public:
	B& operator*() {
		return vec[idx];
	}

	B* operator->() {
		return &vec[idx];
	}
private:
	int idx;
	vector<B> vec;
};

class B {
public:
	void frobnicate();
};

void test(A& a) {
	(*a).frobnicate();
	a->frobnicate();
}
Thanks, maybe I am overthinking this.

@belfegor: what do you mean?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

I wanted to suggest CRTP but realize it could not work with a vector.

Ah ok, thanks

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

If you are wanting to directly call arbitrary member functions on every object, you can pass in member functions as function pointers:


#include <iostream>
#include <vector>

class ClassB
{
public:
    void SetThingie(const std::string &str);
    void DoSomething(float f, int i);
};

class ClassA
{
public:
	ClassA(int objCount)
	{
		this->objects.resize(objCount);
	}
	
	template<typename MemberFunc, typename ...Args>
	typename std::enable_if<std::is_member_function_pointer<MemberFunc>::value, void>::type
    CallMemberFuncOnObjects(MemberFunc memberFunc, Args&& ...args)
    {
    	int id = 0;
    	
    	for(auto &object : this->objects)
    	{
    		std::cout << "ClassB #" << ++id << " = ";
    		(object.*memberFunc)( args... );
    	}
    	
    	std::cout << std::endl;
    }

private:
    std::vector<ClassB> objects;
};

void ClassB::SetThingie(const std::string &str)
{
    std::cout << str << std::endl;
}

void ClassB::DoSomething(float f, int i)
{
	std::cout << "(" << f << ", " << i << ")" << std::endl;
}

int main()
{
	ClassA myClassA(5);
	
	myClassA.CallMemberFuncOnObjects(&ClassB::SetThingie, "Hello world!");
	myClassA.CallMemberFuncOnObjects(&ClassB::DoSomething, 3.14f, 357);
	
	return 0;
}

[Ideone test code]

The same thing works with just a single object in the array (using currentIndex), but that wouldn't serve much point because you could just access the object and call the function directly, as fastcall22 pointed out.

This topic is closed to new replies.

Advertisement