//
you're reading...
Programming Issues/Tips

Pointers to C++ Class Member Operators

1. Introduction.

1.1 In C++, it is possible to define a class member function type and thereafter declare variables of that type.

1.2 Note that it is also possible to define a class member operator type and thereafter declare variables of that type.

1.3 This article demonstrates how to do this with reference to a simple C++ class.

2. Sample C++ Class with a Member Operator.

2.1 Let’s say we have the following C++ class :

class MyClass
{
public :
	MyClass() :
	  m_int(0)
	{
	}

	~MyClass()
	{
	}

	// Let's say MyClass has a < operator.
	bool operator < (const MyClass& rhs)						
	{
		return (m_int < rhs.m_int);
	}

public :
	int m_int;
};

MyClass is very simple and it contains an integer member “m_int” that is publicly accessible. MyClass also contains a < operator that can be used to compare an instance with another.

2.2 The following is a sample usage of MyClass with special emphasis on the < operator :

MyClass class1, class2;

class1.m_int = 1;
class2.m_int = 2;

// Use MyClass' < operator in a typical way.
if (class1 < class2)
{
	std::cout << "class1 < class2";
}
else
{
	std::cout << "class1 >= class2";
}

3. Type Defining a Class Member Operator.

3.1 To define a type for a class member operator, we would use the C++ typedef keword as we would do for any class member function :

// Typedef MyClass' < operator.
typedef bool (MyClass::* P_LESS_THAN_OPERATOR) (const MyClass& rhs);

3.2 After such a type has been defined, we can declare variables of that type :

// Use the P_LESS_THAN_OPERATOR type to define 
// a variable that points to MyClass' < operator.
P_LESS_THAN_OPERATOR less_than_operator_ptr = &MyClass::operator <;

Note that here, there is nothing special about the fact that “less_than_operator_ptr” is made to point to the < operator of the MyClass class.

It could have been made to point to any other member function that fits the definition of P_LESS_THAN_OPERATOR which is essentially a MyClass member function which takes a const reference to another MyClass instance and which returns a bool.

3.3 The following is a sample code that demonstrates how to use “less_than_operator_ptr” :

// Use the P_LESS_THAN_OPERATOR type to define 
// a variable that points to MyClass' < operator.
P_LESS_THAN_OPERATOR less_than_operator_ptr = &MyClass::operator <;

class1.m_int = 3;
class2.m_int = 2;

// Use less_than_operator_ptr as you would any 
// class function pointer.
if ((class1.*(less_than_operator_ptr))(class2))
{
	std::cout << "class1 < class2";
}
else
{
	std::cout << "class1 >= class2";
}

Note the special .* notation that is used to invoke a member function of an instance of a class.

4. Conclusion.

4.1 C++ is truly an amazing language with such tremendous flexibility.

4.2 I hope this article will motivate readers to experiment with pointers to class member operators.

 

Advertisements

About Lim Bio Liong

I've been in software development for nearly 20 years specializing in C , COM and C#. It's truly an exicting time we live in, with so much resources at our disposal to gain and share knowledge. I hope my blog will serve a small part in this global knowledge sharing network. For many years now I've been deeply involved with C development work. However since circa 2010, my current work has required me to use more and more on C# with a particular focus on COM interop. I've also written several articles for CodeProject. However, in recent years I've concentrated my time more on helping others in the MSDN forums. Please feel free to leave a comment whenever you have any constructive criticism over any of my blog posts.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: